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 ()



LinkBack URL
About LinkBacks


