Thread: Character Counting

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    13

    Question Character Counting

    How would I count the number of characters in a string?

    Example:

    GHfgHG235, etc.
    and I want to know the number of times G, H, f, g, 2, etc., appears in the string?

    I would assume that the string could be read into an array and then the counting would be no problem. But specifically how would I read the string into an array?

  2. #2
    </life>
    Join Date
    Oct 2004
    Posts
    83
    A string is a character array, each character in a string can be accessed.. string[1], string[2] etc..

    The end of a string is signified by the '\0' character.. you could go through every character in the string until the character is '\0'
    Microsoft is merely an illusion, albeit a very persistant one.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How would you do it on paper? Write out the steps, then turn it into code.

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

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by glider_pilot123
    But specifically how would I read the string into an array?

    You'd likely want to use either the scanf or the fgets functions:

    Code:
    char string[80];
    scanf("%s",string);
    Code:
    char string[80];
    fgets(string,sizeof(string),stdin);
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    13
    Thanks hk_mp5kpdw. Being able to see the start of the code helps wonders.

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    But specifically how would I read the string into an array?
    The string is an array. If you use any of the standard C input functions, you can get a string from the user in the form of a null-terminated array. You can also specify a string literally. If i remember correctly it goes something like this:
    Code:
    char string[10] = "Abcdefgh";
    Then just use a for loop to cycle through each element. Set it to stop when that character is null. The final value of the loop counter is the string length +/- 1, depending on how you implement it. In each cycle of the loop, just figure out your own way to keep track of the number of times a character has been called. I can think of a realy easy way involving an array of ints, indexed by ASCII numbers.

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by hk_mp5kpdw
    Code:
    char string[80];
    scanf("%s",string);
    This is the same as gets()... Possible buffer overflow... i think

    There's another active thread where the same subject is being treated.. Don't browse the page to the end unless you want code spoilers...
    Last edited by xErath; 11-15-2004 at 06:01 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. character set translation
    By password636 in forum C Programming
    Replies: 1
    Last Post: 06-08-2009, 11:45 AM
  2. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  3. get wide character and multibyte character value
    By George2 in forum C++ Programming
    Replies: 27
    Last Post: 01-27-2008, 05:10 AM
  4. pipe
    By smart girl in forum C Programming
    Replies: 4
    Last Post: 04-30-2006, 09:17 AM
  5. Character counting program
    By TankCDR in forum C++ Programming
    Replies: 5
    Last Post: 04-05-2002, 10:01 PM