C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old December 1st, 2005, 09:49 PM
highlander2k5 highlander2k5 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2005
Posts: 14 highlander2k5 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 12 m 31 sec
Reputation Power: 0
Another string question

I want to add up the sum of a string and I was wondering how you do that? For example if a user inputs the word "dog" into a string. It should sum to 314 if you look at the ascii code table.
d = 100, o = 111, g = 103

Reply With Quote
  #2  
Old December 2nd, 2005, 03:40 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Click here for more information.
 
Join Date: Sep 2005
Posts: 790 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 4 Days 38 m 39 sec
Reputation Power: 4
With a c-style string (char * not c++-string) it's simple:

c Code:
Original - c Code
  1. char * str = "dog";
  2. sum = 0
  3. for( i=0; i<strlen(str); i++ )
  4. {
  5.   sum += str[i];
  6. }


p.s. (for c++ you could use str->c_str() to get a 'c string')

Reply With Quote
  #3  
Old December 2nd, 2005, 07:16 PM
Geo.Garnett's Avatar
Geo.Garnett Geo.Garnett is offline
Registered Loser
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: Retardation Nation...
Posts: 347 Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level)Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 3 h 13 m 45 sec
Reputation Power: 4
Send a message via AIM to Geo.Garnett
Just a thought ICON but wont strlen only count the amount of characters in the string and not necessarily give you the ascii amount for it? But If you want to count the characters in a string in C++ you could also just use str.length() str being the string name!

Read this Highlander, I submited it in your last post on strings and libraries.strings
__________________
---Official Member Of The Itsacon Fan Club---
Give a man a fish and he will eat for a day. Teach a man to fish and he will sit in a boat all day drinking beer.

Last edited by Geo.Garnett : December 2nd, 2005 at 07:19 PM.

Reply With Quote
  #4  
Old December 3rd, 2005, 05:02 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Click here for more information.
 
Join Date: Sep 2005
Posts: 790 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 4 Days 38 m 39 sec
Reputation Power: 4
Yes if you want to count the number of characters you could use strlen or the other one. I'm just using strlen to iterate over all characters in the string. The ascii value is retrieved in the sum += str[i]; part, str[i] being the ascii value of ith character

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Another string question


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT