Count the number of letter "a" on a sentence

This is a discussion on Count the number of letter "a" on a sentence within the C Programming forums, part of the General Programming Boards category; We had a 20 minute seatwork, and nobody solved this riddle: Write a source code in C language that asks ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    60

    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 ??

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    10
    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

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    60

    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 !!

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    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++

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    60

    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 !!!!

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    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.

  7. #7
    Registered User
    Join Date
    Jun 2003
    Posts
    124
    >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.
    Loading.....
    ( Trying to be a good C Programmer )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Count number of lines in a text file
    By eXeCuTeR in forum C# Programming
    Replies: 3
    Last Post: 04-30-2008, 01:46 AM
  2. Count number of letters and numbers in a file
    By jtullo in forum C Programming
    Replies: 2
    Last Post: 04-21-2008, 01:33 AM
  3. Need help with this compiler error
    By Evangeline in forum C Programming
    Replies: 7
    Last Post: 04-05-2008, 09:27 AM
  4. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM
  5. count the number of token
    By rsanga1 in forum C Programming
    Replies: 0
    Last Post: 10-02-2001, 11:48 AM

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