Thread: Help with counting letters

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    72

    Help with counting letters

    Hello,
    I have looked at some of the other threads that are similar to my dilemma, but they don't seem to capture what I'm experiencing. I basically have to make a program (specifically using the string class versus C-string) that allows a user to type a line of text. It then has to figure out how many words there are in the line of text and give a count of letters (making sure that lower case and upper case are seen as the same letter). The following code doesn't include my part for the word counting, as I have that under control. The part I can't quite get is the letter count. Here's my code thus far:

    Code:
    #include <iostream>
    #include <string>
    #include <cstring>
    
    using namespace std;
    
    
    int main ()
    
    {
    	int letter_count;
    	int words=0;
    	int counter[26]={0};
    	
    	string line;
    
    	cout << "Enter a line of text:\n";
    
    	getline(cin,line);
       
    	for (letter_count=0; letter_count <=line.length(); letter_count++)
    		
    		{
    		if (isalpha(line[letter_count]))
    		{
    			line[letter_count]=tolower(line[letter_count]);
    			
    			cout << counter[line[letter_count]] << endl ;
                counter [line[letter_count] - 'a']++;
    		} else
    		{
    			words=words+1;
    		}
    return 0;
    }
    Sample output:

    Enter a line of text:
    I say hi.

    i 1
    s 44
    a 2
    y 0
    h 112
    i 1

    I assuming that instead of giving counts for each of these letters, it is displaying the ASCII representation instead. I am not sure how to get to do the following instead:

    Enter a line of text:
    I say hi.

    i 2
    s 1
    a 1
    y 1
    h 1

    Any suggestions/guidance that could be provided on this would very very much appreciated.

    Thank you!

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    try something like this (im not sure if it will work):
    Code:
    ...
    char*alphabet="abcdefghijklmnopqrstuvwxyz";
    int occurances[26];
    
    for(int i=0;i<26;i++)
       occurances[i]=0;
    
    for(int i=0;i<26;i++)
    {
       for(int x=0;x<strlen(STRINGNAME);x++)
       {
          if(!strcmpi(alphabet[i],STRINGNAME[x])
             occurances[i]++;
       }
    }
    ...
    like i said, i don't know if it'll work, because strmpi is used for strings, and i'm passing a single element of a string to it... i can't remember if it will work...

    //edit: if your string is going to be longer than 26 chars, put the stringname part on the outside loop and the 26 part on the inside...
    Last edited by major_small; 10-23-2003 at 08:31 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    28
    Code:
    #include <iostream>
    #include <string>
    #include <cstring>
    
    using namespace std;
    
    int main ()
    {
      int letter_count;
      int words=0;
      int counter[26]={0};
    	
      string line;
    
      cout << "Enter a line of text:\n";
    
      getline(cin,line);
       
      for (letter_count=0; letter_count <=line.length(); letter_count++)
        {
          if (isalpha(line[letter_count]))
    	{
    	  line[letter_count]=toupper(line[letter_count]);
    			
    	  counter[line[letter_count]-'A']++;
    	} 
          else
    	{
    	  words=words+1;
    	}
        }
    
      for (letter_count=0; letter_count<=25; letter_count++)
        {
          if(counter[letter_count] > 0)
    	{
    	  cout << char(letter_count+'A') << " " << counter[letter_count] << endl;
    	}
        }
    
      return 0;
    }

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    72
    Thank you both very very much!! It's working like a charm!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. counting letters in string
    By CGbiginner in forum C Programming
    Replies: 1
    Last Post: 03-31-2008, 11:47 AM
  2. Counting uppercase and lowercase letters in a text
    By Tintu in forum C Programming
    Replies: 2
    Last Post: 02-06-2008, 10:15 PM
  3. Counting letters and digits
    By FeNCinGeR in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2006, 11:39 AM
  4. weird response when counting the letters.
    By Finchie_88 in forum C++ Programming
    Replies: 8
    Last Post: 04-01-2005, 12:41 PM
  5. I need help counting letters., folk!
    By correlcj in forum C Programming
    Replies: 7
    Last Post: 07-14-2002, 12:31 PM