need to count individual letters in input

This is a discussion on need to count individual letters in input within the C Programming forums, part of the General Programming Boards category; hi there, i'm new to c and I've been trying for a while now to achieve the following task, I'm ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    3

    need to count individual letters in input

    hi there, i'm new to c and I've been trying for a while now to achieve the following task, I'm hoping someone here might be able to help me out. Ok, so my program firstly allows the user to enter a string, now I need figure out how to count the different letters and numbers that are in that string. for example, if the user entered "Hello", then my program should print out the letters that are in that string, ie. e, h, l, o. My program should also print out how many times each of those letters are present, so in the case of "Hello", the program should end up printing out e - 1, h - 1, l - 2, o - 1

    any help here would be much appreciated. I've been trying to use switch/case statements but still haven't managed to accomplish the task.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There are a few ways to do this. You can...

    Loop through the string, and as you encounter a character, increment the approptriate variable as you encounter that character. IE: Use an array big enough to hold every character, start their count all at zero, and every time you find one, increment that spot.

    Loop through the string, take a character as you encounter it, and count every occurance of that character in the string. Repeat until finished.

    One takes more overhead as far as memory goes; the other takes more loops through the string.

    I'm sure there are other interesting variants, but those are probably the two most common.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    the only problem with the second way is it would probably output each char that is in the string multiple times, multiple times. lol for example, input Hello ; output H - 1, e - 1, l - 2, l - 2, o - 1
    you could always store each char in a string and as you go through the input, check to see if the letter your at has been processed already..
    any other ways you can think of?
    Registered Linux User #380033. Be counted: http://counter.li.org

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The problem perhaps with the way you are implementing it. Not the way I implement it.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,666
    I was thinking maybe we should sort the string first, or rather sort the individual characters in the string. For example, "Hello" becomes "eHllo". Now that the string is in order it's just that you need to match a character and count. If the match fails, count the current character and try to match it with the next one until you reach the NUL terminator.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Sorting would work nicely too. I hadn't thought of that. That's a nice one.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    3
    ok here's my next dilemma, when i want to check the characters I want "A" to be equivalent to "a" so basically if I input "Aa" it should print out A - 2.

    Is there any methods that can convert strings to lower or uppercase?

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    tolower perhaps? There are many functions in <ctype.h> that preform similar tasks.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    3
    sweeeeet im finally making some progress, ok so i've managed to print the letters once and provide a count for each letter. the next thing I'm trying to achieve is to distinguish between numbers, letters and punctuation. are there any methods that will allow me to determine if a character is a number or not? the way i figure my program should work is to test if the character is a number or letter. if it's neither then i can just count it as being punctuation. thanks for all the help so far

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Look up the functions in <ctype.h> as mentioned.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    20
    you can use isdigit() function if I am not wrong it should be included in <ctype.h> as quzah said above.
    Code:
    //Example reads in a character and checks to see if it is a digit
    #include <cctype>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        char a_char;
        cin>>a_char;
        if(isdigit(a_char))
        {
            cout<<"Is a digit!";
        }
        else
        {
            cout<<"Is not a digit!";
        }
    }
    here is your function I mentioned about, maybe you can modiyf it to use most efficiently with you program.
    Last edited by mass; 05-22-2006 at 05:48 AM.

  12. #12
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Quote Originally Posted by mass
    you can use isdigit() function if I am not wrong it should be included in <ctype.h> as quzah said above.
    Code:
    //Example reads in a character and checks to see if it is a digit
    #include <cctype>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        char a_char;
        cin>>a_char;
        if(isdigit(a_char))
        {
            cout<<"Is a digit!";
        }
        else
        {
            cout<<"Is not a digit!";
        }
    }
    here is your function I mentioned about, maybe you can modiyf it to use most efficiently with you program.
    Try to post C code on the C board.

  13. #13
    Registered User
    Join Date
    May 2006
    Posts
    20
    the code is taken from this site and the link is,
    http://www.cprogramming.com/fod/isdigit.html
    If youre talking about cin>> and cout>> I just wanted to show the function did not look if its written in C or not. thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. For loop problems, input please.
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-22-2007, 03:54 AM
  3. input question
    By piyush_v in forum C Programming
    Replies: 9
    Last Post: 04-12-2007, 07:09 AM
  4. Count the letters of a string.
    By blackjs in forum C++ Programming
    Replies: 1
    Last Post: 10-17-2001, 10:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21