# Count the number of letter "a" on a sentence

• 07-27-2003
imbecile in C
Count the number of letter "a" on a sentence
We had a 20 minute seatwork, and nobody solved this riddle:

Write a source code in C language that asks a user to input a sentence, then (the program) displays the total number of letter
" a " on that particular sentence.

I believe its the lowercase letter "a"..

---
Had I solved it, I would have got 100% on my recitation...

My idea was to treat the character as an integer then count them all,.. but ??

Does that make sense ?? How would I have coded that ??

We should all stick on the <stdio.h> library.. (that's what our Instructor told us) ...

Any suggestions or Codes to this matter ??
• 07-27-2003
zbap
Here are some pointers on this

you can use a loop to run through your string and test each character. If it is equalled to 'a' you increment a variable previously initialised to 0.

Remember all characters are non-zero in value except for '\0' which you can use in an expression to terminate your loop
• 07-27-2003
imbecile in C
Oooppss.. I slipped !!
Im sorry,.. I couldnt find the right functions or techniques for this...

A loop,.. loop yes !! But I couldnt find the approach for counting
characters like integers,..

please share a little code,.. or little example !!

Thanks !!
• 07-27-2003
mart_man00
You have the right idea. A letter is just a number.

Code:

```#define    MAX_LENGTH      256 #include <stdio.h> int main() {     char sentance[MAX_LENGTH];      // Holds the sentance     int x = 0;                      // Holds the current posistion for the search     int y = 0;                      // Holds total number of a's found         printf("Enter a sentance\n");  // Gets input     fgets(sentance, MAX_LENGTH, stdin);         while(sentance[x] != '\0') {    // Loop threw sentance         if(sentance[x] == 'a')      // Chech to see if the current character is a a             ++y;                    // It is, add one to y                     ++x;                        // Update posistion     }         printf("\nThere are %i letter a's in your sentance\n", y);  // Print answer     return 0; } // Wonder why I did c++ comments in c code when I dont do c++```
• 07-27-2003
imbecile in C
Oh my.... way to go Mart man!!
That's a real help dude...

I couldn't really imagine myself doing such a code (yet)..

but I have to research some stuff like that fgets() function, '\0'... and using [](i think they call it arrays),..

we haven't talked about such topics yet... and I just browse my books, and get some ideas..

Great Help... I'll have it up my sleeves,..

I think I'll have some ideas on how to count words, white spaces, or other syllables..

Thanks dude !!!!
• 07-27-2003
mart_man00
cplusplus.com has a great reference section for the standard functions.

'\0' is NULL. Every string (should)ends with a '\0' to show thats the end.
• 07-27-2003
money?
>we haven't talked about such topics yet... and I just browse my books, and get some ideas..

Then, you shouldn't be happy with an answer like mart_man's. Thy find answer's that fit your knowledge.