Thread: Taking a string and counting number of occurrences of each letter in the string

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    11

    Question Taking a string and counting number of occurrences of each letter in the string

    I need to write a program that reads a string from the keyboard and then outputs the number of occurrences of each letter in the alphabet as it pertains to the string. I also need to be able to count the number of words in the string. So far, here is the code I have come up with.

    #include <iostream.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    int main()
    {
    const int size=100;
    char string[size];
    char *sPtr = string;
    cout <<"Enter a sentence of "<<size<<" characters or less to be analyzed.\n";
    cin.getline(string,'\n');
    return 0;
    }

    If anybody knows of a quick and easy way to accomplish this I would be eternally greatful. Yes this is a homework problem if anyone wonders, but I'm not looking for a handout. Thanks for everybody's help in advance!!!

  2. #2
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    you're on the right track.....to tell you the truth the solution is acutally very easy...

    you already obtained a sentence of 100 characters and you stopped when a new line is encountered....

    first you need a variable that would stand for a letter.

    char AletterVariable;

    also you need a counting variable so you can keep track of how many times this letter occurs.......

    int SomeCounter;

    as well, you need another counting variable to stand for a number of words in your string.....

    int WordCount;


    and the final step, the gist of the program is to make for example 2 functions.....one that that compares a character array, one by one, by sending each character to a function, and if such a character is the one desired, then the counting variable is increased by one...

    i.e:

    int AlphabetFunction(char x)
    {
    if (AletterVariable == x)
    ++SomeCounter;

    return SomeCounter;
    }


    another function and also the "gist" of the program is to increase your word counting variable by one every time you encounter a "space" .......now, a space you can refer to as simply char(32)......if my memory serves me correctly.......

    .....

    and a final clue.......

    for your input try an iterative solution with something like this.....

    getline(cin, string[z], char(32));

    hint: z ..... increases as you go through your array......

    .....


    that should you give you a good idea of what to do.....

    good luck


    Regards,
    matheo917

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    11
    Thanks for your help so far, however, when trying to use what you have supplied, I am a little bit confused. First of all, I am getting an error when I try to compile that states:

    C:\Documents and Settings\jpg9981\My Documents\COMP3000\letters.cpp(25) : error C2664: 'class istream &__thiscall istream::getline(char *,int,char)' : cannot convert parameter 1 from 'char' to 'char *'
    Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast

    Also, I don't understand how the function you showed me will do what I need it to do. I guess I just am not familiar enough with syntax and the code to understand what is going on. If you could please help me with this it would really be great. Also, how will I call the function to match the letters and get the output to show each letter of the alphabet and then the number of occurrences? I know there are a lot of questions I'm asking but I am just in the dark with this stuff. Here is my updated code for now:

    #include <iostream.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    int AlphabetFunction(char);

    const int size=100;
    int WordCount;
    int SomeCounter;
    char AletterVariable;

    int main()
    {
    char string[size];
    char z;
    cout <<"Enter a sentence of "<<size<<" characters or less to be analyzed.\n";
    cin.getline(string[z],'\n');
    return 0;
    }
    int AlphabetFunction(char letter)
    {
    if (AletterVariable == letter)
    ++SomeCounter;
    return SomeCounter;
    }

  4. #4
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    First of all ..... the sample code and all the insights are just a way of showing you direction in where to go in your program. DON'T use the exact same wording and algorithms.... they're not designed specificly for your program....

    Second of all....the error measage signifies that it can't conver a character pointer(*char) to a character(char)...you have to read into it a little more, these measages are usually very helpful and descriptive..... (get rid of the "star" *)

    Third of all......... my clues were supposed to just help you with structuring your program, and the rest was just all about the algorithms you would use.

    Now here's a good idea.....whenever you have a programming problem, never start with coding (unless it's a simple "hello world" program).....
    In your example.... write down the problem and expectations on paper and then (in this case) try to solve a problem on the piece of paper as in just a human being would to solve it,

    for instance.... you have a sentence ... you look at each letter one by one...and you compare it to your desired letter.....if their the same write or add 1 onto your paper.....you go to the next letter and do the same....you stop when you encounter a space....that's when you write or add 1 onto your paper where it says "word count"..... then you continue onto the next word....and do the same "letter by letter" and "word by word" until you encounter an end of the line "'\n"....

    now i just presented to you your solution...... the trick is now puting into C++.........if the program is realatively large (bigger then one or couple functions) then i RECOMMEND .....on the separate piece of paper writing a "structure tree" or a function tree and have "lines" branches link from one function to another, that is however one function needs to be called by another.......

    after that if everything seems clearer...you can start coding......one function at a time....

    of course there's a lot more into designing a software application, but this is what you should start with...

    now.......the last thing....... of course, it would be a lot easier for me to just type up some code and send it to you, however the point of this board and pretty much the idea of "programming" is not to do entire program for someone.....that's why i took time to show you the basics.....to tell you the truth this is just a simple problem solving skills, and not really that much innovative coding.......

    the simple scenerio that i described to you should help with virtually all your programs and pretty much every programmin language out there....

    however, if you try your best to do the program and at the end you know that you accomplished it pretty much by yourself, the feeling is more rewarding than just a smiple smile after some quickly send you the program.....


    Good LUck.....

    Regards,
    matheo917

    ps. if you need additional help, i reccomend trying it this way, and then if u encounter a problem, let's try to approach it 1 (one) function or algorith at a time, instead of just the whole problem.......you can e-mail me with any additional questions(see my profile), or just post them......

  5. #5
    Unregistered
    Guest
    while((ch = cin.get()) != '\n')
    {
    if(isspace(ch))
    word++;
    else
    {
    ch = tolower(ch);
    array[ch - 'a']++;
    }
    }

    for(ch = 'a'; ch < = 'a' + 26; ch++)
    {
    cout << "the letter " << ch << " appears in the phrase " << array[ch - 'a'] << "times" << endl;

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    11
    Thanks for all the help, I only have a couple of questions now. First of all, I can't figure out how to count letters regardless of whether they are upper or lower case. Secondly, I can't figure out how to develop a way to increment my word counter so that I know how many words are in the sentence that was input. After I get those two things figured out I will be finished with this program and so relieved.

  7. #7
    Unregistered
    Guest
    if you are counting the letters as lower case pass each char as it is read to to tolower(), if you are counting letters in their uppercase form then use toupper().

    If char == ' ';
    increment the counter keeping track of the number of words by 1.

  8. #8
    Unregistered
    Guest
    {
    if(isspace(ch))
    word++;
    else
    {
    ch = tolower(ch);
    array[ch - 'a']++;
    }
    }

    what does the isspace(ch) here do?

  9. #9
    Unregistered
    Guest
    isspace() is a function that checks to see if the given char is a whitespace char like space, tab, newline char, etc. If it is it returns a non-zero value. If it isn't it returns 0. When used in a conditional 0 means false and non-zero means true. So the if(isspace(ch)) means if ch is a space or other white space char like newline then increment the word count, else change the char to lower case if it is a letter and increment the appropriate counter in the array of counters.

  10. #10
    Registered User
    Join Date
    Feb 2011
    Posts
    1
    i have the same problem too.. the output would be like this:
    *******************
    Enter a word: Denie
    Enter a letter: E
    the number of occurrence of the letter E is 2.
    ******************************************

    what codes do i need to make a program of this. .???

  11. #11
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    1. Input a string.
    2. Get the resulting character.
    3. Get the length of string.
    4. Start a loop and within the loop;
    i. A conditional statement that will find whether given character is present or not. If yes, increment in the counter, else do nothing.
    5. After loop exits, prints the counter value.
    I hope this algorithm will solve your problem....
    Good luck..

    And if you want to make it case sensitive, try comparing ASCII values..

  12. #12
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    9 years old.

    Impressive.
    Woop?

  13. #13
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Quote Originally Posted by prog-bman View Post
    9 years old.

    Impressive.
    What?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-25-2009, 08:28 PM
  2. counting letter occurences in a string
    By pjr5043 in forum C++ Programming
    Replies: 35
    Last Post: 05-05-2008, 09:18 PM
  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. Error counting occurrences
    By nick048 in forum C Programming
    Replies: 1
    Last Post: 04-09-2007, 02:08 PM
  5. Counting number from a random file
    By kamisama in forum C Programming
    Replies: 42
    Last Post: 02-22-2005, 05:16 PM