Thread: counting letter occurences in a string

  1. #31
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You tell us how you plan on solving the problem. Yes, there are lots of different hints here, but if you understand them, then it is not too confusing.

    You have a start. You are outputting the characters in the string that are alphabetic characters.

    These are the steps I would follow:

    1. Create an empty void count(const char myStr[]) function that does nothing. Compile and make sure the code compiles.
    2. Call that function from main the way the assignment demonstrates. Make sure that compiles.
    3. Take your code that you have now that checks isalpha and add it to the count function. Make sure that compiles. Then run it and make sure you get something you expect.
    4. Now comes the harder part. How do you define the count array. First, don't call it count, because the name of the function has to be count (according to the assignment). So give it a different name. In your current code you made it an array of int. That is good, because the counts will be ints. You specified 256 as the size. That can work but is not the only way. Do you know why you picked 256?
    5. The answer to the previous question will tell you what to do here. Instead of using cout to output the character at myStr[i] you need to increment the proper count. How you do it depends on how you intend to solve the problem. Work on understanding your solution in English (not code), and if you get stuck then ask about that part here.

  2. #32
    Registered User
    Join Date
    Jan 2008
    Posts
    37
    ok i have this code .. i just want to change one thing, instead of entering a string .. i'd like to have this be the default string "ABcaBadd*eekjdfefdeg,TTew44Tt"

    Code:
    #include <iostream>
    #include <cctype>
    using namespace std;
    
    //counts how many each character shows
    
    void count(const char myStr[]){
    	for (int x = 97; x < 123; x++){
    		int count1 = 0;
    		for (int i =0; myStr[i] != '\0' ; i++){
    			if (myStr[i] == (char)x){
    				count1++;
    			}
    		}
    		if (count1 != 0)
    		cout << (char)x << " appears " << count1 << " times\n";
    	}
    }
    
    int main(){
    
    	char phrase1[500];
    
    	cout << "What is your String?\n";
    	cin.getline (phrase1, 500);
    
    	const int x=500;
    	char myarray[x];
    
    	int c = 0;
    	//takes each char from the string and check to see if it is both 
    	// not a space and is a alphabetical char
    	for(int i = 0; phrase1[i] != '\0'; i++){
    		if (isalpha(phrase1[i])){
    			myarray[c]=tolower(phrase1[i]);
    			c++;
    		}
    	}
    	//reassigns null terminator to the end of the array
    	myarray[c]= '\0';
    
    	count(myarray);
    
    	return 0;
    }

  3. #33
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You can initialize phrase1 with whatever you want and skip the input part.

    BTW, what is 97 and 123?

  4. #34
    abyss - deep C
    Join Date
    Oct 2007
    Posts
    46
    Quote Originally Posted by pjr5043 View Post

    //counts how many each character shows
    void count(const char myStr[]){
    for (int x = 97; x < 123; x++){
    int count1 = 0;
    for (int i =0; myStr[i] != '\0' ; i++){
    if (myStr[i] == (char)x){
    count1++;
    }
    }
    if (count1 != 0)
    cout << (char)x << " appears " << count1 << " times\n";
    }
    }
    [/CODE]
    This can be done in a better way!!!
    Code:
    void count(const char myStr[]){
        declare an array of 26 integers to store the count of each alphabet
    
        initialize the array variables to zero    
    
        loop until you reach the end of myStr[]
            if current variable in myStr[] is an alphabet
                increment the corresponding index in the integer array
        
        loop through the integer array and print the values in each index
    }
    cheers
    maverix

  5. #35
    Registered User
    Join Date
    Jan 2008
    Posts
    37
    thank you for your help .. and daved the 97 and 123 was so taht it only counts 26 times .. the amount of letter that are in the alphabet i guess, i recieved that from another student because i was having so much trouble with the program

  6. #36
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Thanks. I figured you didn't write the code but was hoping you understood what the number meant. (Why not 96 and 122 or 0 and 26?) In fact 'a' would normally be better than 97, but for this assignment it doesn't matter.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 03-07-2011, 01:24 AM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. Help with hw problem: counting characters in a string
    By pinkfloyd4ever in forum C++ Programming
    Replies: 11
    Last Post: 11-04-2007, 11:18 PM
  4. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM