Thread: Character counting

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    7

    Unhappy Character counting

    Hey guys... Just wondering if you could give me a hand here... im trying to work out how to count how many characters there are in a string that is read from a text file... it basically reads in some peoples names, and obviously each name is going to be different lengths, and i need to work out how long each name is by counting the characters so i can get the formatting of the output right...

    any help would be much appreciated...

  2. #2
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    I believe there is a function "size()" that could do what you want

  3. #3
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Quote Originally Posted by Welshy
    I believe there is a function "size()" that could do what you want
    That's right. Or you can use length() as well. Here's simple code reading user input (it's pretty much same with files)
    Code:
    #include <iostream>
    #include <string>
    
    
    using namespace std;
    
    int main()
    {
    	string str;
    	getline(cin,str);
    	cout<<"There are "<<flush<<str.length()<<" characters!"<<endl;
    	system("pause");
    	return 0;
    }
    - Micko
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    there's quiete a few ways to do this. but really you should use methods 1 and 2 (below) most of the time, if not all of the time. i just wanted to show the last 2 so you can see how it works.

    Code:
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	char stringArray[] = "Blah blah blah";
    	string stringObject("hum dee dum");
    
    	//Method 1 and 2
    	cout << "stringArray size: " << strlen(stringArray) << endl;
    	cout << "stringObject length: " << stringObject.size() << endl;
    	//stringObject.size() and stringObject.length() ARE the same
    
    	//Method 3
    	char *p = stringArray;	
    	int i = 0;
    	while(*p++)
    		i++;
    	cout << "stringArray length: " << i << endl;
    
    	//Method 4
    	i = 0;
    	string::iterator si = stringObject.begin();
    	for(; si != stringObject.end(); si++, i++);
    		
    	
    	cout << "stringObject length: " << i << endl;
    }
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. character set translation
    By password636 in forum C Programming
    Replies: 1
    Last Post: 06-08-2009, 11:45 AM
  2. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  3. get wide character and multibyte character value
    By George2 in forum C++ Programming
    Replies: 27
    Last Post: 01-27-2008, 05:10 AM
  4. pipe
    By smart girl in forum C Programming
    Replies: 4
    Last Post: 04-30-2006, 09:17 AM
  5. Character counting program
    By TankCDR in forum C++ Programming
    Replies: 5
    Last Post: 04-05-2002, 10:01 PM