Thread: Help!

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

    Help!

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

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    why dont you use nested loops to loop through your string to do the letter count.you need two loops. One for the number of elements in array and another for the char to check for. Remember that a could appear as 'a' or 'A' etc. so maybe make a copy of your array and toupper() it to make that problem disappear.Hell if you are going to make a copy of your array then you may as well sort it as well as that will make the letter counting dead easy!
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    43
    Oh, my god! This big code for that simple thing?

    #include <string.h>

    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 Nums['Z' - 'A' + 1];
    memset(Nums, 0, sizeof(Nums));
    strupr(string);
    const char* pEnd = string + strlen(string);
    for (const char* pCurr = string; pCurr != pEnd; ++pCurr)
    {
    if (*pCurr >= 'A' && *pCurr <= 'Z')
    ++Nums[*pCurr - 'A'];
    }

    for (int i = 0; i < sizeof(Nums)/sizeof(*Nums); ++i)
    {
    if (Nums[i])
    cout << char(i + 'A') << ' ' << Nums[i] << ' ';
    }

    cout.flush();
    return 0;
    }

Popular pages Recent additions subscribe to a feed