Thread: Still not getting it?

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    9

    Still not getting it?

    I am trying to count the occurences of each letter of the alphabet for a line of text.

    here is what i have so far.

    your help is greatly appreciated.

    # include <iostream.h>
    # include "string.h"

    void alphabet( char [] ); //functions prototype

    int main()
    {
    int count = 0;
    char string[256];

    cout <<"Please enter 1 line of text from the keyboard. \n";
    cout <<"Please hit enter when you are done.\n\n";
    cin.getline(string, 256, '\n'); //The user input goes into string

    int numletters [25];//initalizes an array for the letters of the alphabet
    for (int x = 0; x < 25; x++)//sets the letter counts to zero
    numletters[x] = 0;

    for(int i = 0; i < 256; i++)
    {
    if (string[i] == 'a')
    numletters[0]++;
    if (string[i] == 'b' || 'B')
    numletters[1]++;
    if (string[i] == ' ')
    i++;
    }
    cout <<"A " << numletters[0] << "\n";
    cout <<"B " << numletters[1] << "\n";


    char *tokenPtr;
    tokenPtr = strtok( string, " " );

    while ( tokenPtr != NULL )/* The following code counts the words or tokens in a sentence.*/
    {
    tokenPtr = strtok( NULL, " " );
    count = count + 1;
    }//end while ( tokenPtr != NULL )

    cout << "Word Count: " << count;

    return 0;
    }//end int main ()

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this:
    Make numletters 26 elements for a-z.

    int numletters [26];//Make this 26
    for (int x = 0; x < 26; x++)//Make this 26 times thru loop
    numletters[x] = 0;

    for(int i = 0; i < strlen(string); i++) //Use strlen here
    {
    if (string[i] == 'a')
    numletters[0]++;
    if (string[i] == 'b' || 'B')
    numletters[1]++;
    if (string[i] == ' ')
    i++;
    }

  3. #3
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    ..or to save yourself some typing you could doo something like -

    Code:
    #include <iostream>
    
    using namespace std;
    
    
    int main()
    {
        
    	int numletters [26] = {0};
    
    	char string[]="Zen";
    
    	for(int i = 0; i < strlen(string); i++)  
    	{ 
    		char c = tolower(string[i]);
    		numletters[c -'a']++;
    	}
    
    	for(int j=0;j<26;j++)
    		cout << char('a'+j) << " - " <<numletters[j]<< endl;
    
    
    
        return 0;
    }
    zen

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    9
    that makes each letter equal 2

  5. #5
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    No it doesn't. As you seem to be having problems here it is in your code -

    Code:
    #include <iostream> 
    #include <cstring>
    #include <cstdlib> 
    
    using namespace std;
    
    
    void alphabet( char [] ); //functions prototype 
    
    int main() 
    { 
    	int count = 0; 
    	char string[256]; 
    
    	cout <<"Please enter 1 line of text from the keyboard. \n"; 
    	cout <<"Please hit enter when you are done.\n\n"; 
    	cin.getline(string, 256, '\n'); //The user input goes into string 
    
    	int numletters [26] = {0};
    
    	
    	for(int i = 0; i < strlen(string); i++)  
    	{ 
    		char c = tolower(string[i]);
    		numletters[c -'a']++;
    	}
    
    	for(int j=0;j<26;j++)
    		cout << char('a'+j) << " - " <<numletters[j]<< endl;
    
    
    
    	char *tokenPtr; 
    	tokenPtr = strtok( string, " " ); 
    
    	while ( tokenPtr != NULL )/* The following code counts the words or tokens in a sentence.*/ 
    	{ 
    		tokenPtr = strtok( NULL, " " ); 
    		count = count + 1; 
    	}//end while ( tokenPtr != NULL ) 
    
    	cout << "Word Count: " << count; 
    
    	return 0; 
    }//end int main ()
    zen

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    9
    that is an implicit declaration of function int tolower

Popular pages Recent additions subscribe to a feed