I'm trying to count the occurences of the letters of the alphabet in a string. With this code I get 1 for each letter.
can u help......thank you.
# 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
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 )
int numletters [26];//initalizes an array for the letters of the alphabet
for (int x = 0; x < 26; x++)//sets the letter counts to zero
numletters[x] = 0;
for(int i = 0; i < strlen(string); i++)
{
if (string[i] == 'a' || 'A')
{
i++;
numletters[0]++;
}
if (string[i] == 'b' || 'B')
{
numletters[1]++;
i++;
}
if (string[i] == 'c' || 'C')
{
numletters[2]++;
i++;
}
if (string[i] == 'd' || 'D')
{
numletters[3]++;
i++;
}
if (string[i] == 'e' || 'E')
{
numletters[4]++;
i++;
}
if (string[i] == 'f' || 'F')
{
numletters[5]++;
i++;
}
if (string[i] == 'g' || 'G')
{
numletters[6]++;
i++;
}
if (string[i] == 'h' || 'H')
{
numletters[7]++;
i++;
}
if (string[i] == 'i' || 'I')
{
numletters[8]++;
i++;
}
if (string[i] == 'j' || 'J')
{
numletters[9]++;
i++;
}
if (string[i] == 'k' || 'J')
{
numletters[10]++;
i++;
}
if (string[i] == 'l' || 'L')
{
numletters[11]++;
i++;
}
if (string[i] == 'm' || 'M')
{
numletters[12]++;
i++;
}
if (string[i] == 'n' || 'N')
{
numletters[13]++;
i++;
}
if (string[i] == 'o' || 'O')
{
numletters[14]++;
i++;
}
if (string[i] == 'p' || 'P')
{
numletters[15]++;
i++;
}
if (string[i] == 'q' || 'Q')
{
numletters[16]++;
i++;
}
if (string[i] == 'r' || 'R')
{
numletters[17]++;
i++;
}
if (string[i] == 's' || 'S')
{
numletters[18]++;
i++;
}
if (string[i] == 't' || 'T')
{
numletters[19]++;
i++;
}
if (string[i] == 'u' || 'U')
{
numletters[20]++;
i++;
}
if (string[i] == 'v' || 'V')
{
numletters[21]++;
i++;
}
if (string[i] == 'w' || 'W')
{
numletters[22]++;
i++;
}
if (string[i] == 'x' || 'X')
{
numletters[23]++;
i++;
}
if (string[i] == 'y' || 'Y')
{
numletters[24]++;
i++;
}
if (string[i] == 'z' || 'Z')
{
numletters[25]++;
i++;
}
if (string == NULL)
i = 256;
}
cout <<"A " << numletters[0] << "\n";
cout <<"B " << numletters[1] << "\n";
cout <<"C " << numletters[2] << "\n";
cout <<"D " << numletters[3] << "\n";
cout <<"E " << numletters[4] << "\n";
cout <<"F " << numletters[5] << "\n";
cout <<"G " << numletters[6] << "\n";
cout <<"H " << numletters[7] << "\n";
cout <<"I " << numletters[8] << "\n";
cout <<"J " << numletters[9] << "\n";
cout <<"K " << numletters[10] << "\n";
cout <<"L " << numletters[11] << "\n";
cout <<"M " << numletters[12] << "\n";
cout <<"N " << numletters[13] << "\n";
cout <<"O " << numletters[14] << "\n";
cout <<"P " << numletters[15] << "\n";
cout <<"Q " << numletters[16] << "\n";
cout <<"R " << numletters[17] << "\n";
cout <<"S " << numletters[18] << "\n";
cout <<"T " << numletters[19] << "\n";
cout <<"U " << numletters[20] << "\n";
cout <<"V " << numletters[21] << "\n";
cout <<"W " << numletters[22] << "\n";
cout <<"X " << numletters[23] << "\n";
cout <<"Y " << numletters[24] << "\n";
cout <<"Z " << numletters[25] << "\n\n";
cout << "Word Count: " << count;
return 0;
}//end int main ()