Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C program help

  1. Jul 22, 2006 #1
    Hi guys!

    I made a program in C, which count vowels on text, but it doesn't work.
    Thats the code i have writed:

    Code (Text):
    #include <stdio.h>
    #include <string.h>

    int main()
    {
      char buffer[80];
      int counter;

      printf("Enter a line of text: ");
      fgets(buffer, sizeof(buffer), stdin);
       
      for(counter=0; buffer[counter]!='\0'; counter++)
        {
          if(buffer[counter]=='a' && buffer[counter]=='e'
         && buffer[counter]=='i' && buffer[counter]=='o'
             && buffer[counter]=='u')
        continue;
       
          printf("In text, we have %d vowels", strlen(buffer[counter]));
          return 1;
       }
       return 0;
    }
     
    Does anybody have an idea where is the problem?

    Thanks.
     
  2. jcsd
  3. Jul 22, 2006 #2
    Well you are testing if a character from the buffer is 'a' AND 'e' AND 'i' AND 'o' AND 'u'. Of course that is impossible since it can only have one value.
    Second where do you keep track of the number of vowels? You cant use counter for that since it is used to traverse the string. A faster way by the way would be to use pointer arithmetic to traverse the string.
    Also don't you want to print your message after the for loop?
     
    Last edited: Jul 22, 2006
  4. Jul 23, 2006 #3
    Code (Text):

    #include <stdio.h>
    #include <string.h>

    int
    main()
    {
            char buffer[80];
            int vowels = 0;

            printf("Enter a line of text: ");
            fgets(buffer, sizeof(buffer), stdin);

            int i;
            for(i = 0; buffer[i] != '\0'; i++){
                    switch(buffer[i]){
                            case 'a': case 'e': case 'i': case 'o': case 'u':
                            case 'A': case 'E': case 'I': case 'O': case 'U':
                                    vowels++;
                    }
            }

            printf("In text, we have %d vowels\n", vowels);
            return 0;
    }
     
    Your program can not find capitals.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: C program help
  1. Help with a C Program (Replies: 2)

  2. C program help (Replies: 31)

  3. C program help (Replies: 3)

  4. Help in C programming (Replies: 1)

Loading...