1. ## 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. 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'

3. How would you do it on paper? Write out the steps, then turn it into code.

Quzah.

4. 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);```

5. Thanks hk_mp5kpdw. Being able to see the start of the code helps wonders.

6. 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. 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...