counting vowels in string (c language)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ashpats
    New Member
    • Sep 2008
    • 5

    counting vowels in string (c language)

    Hey everyone,

    I am supposed to write a program that counts the number of vowels in string of characters.
    It must include "Struct counter_t (it must be declared as in the code i've written)" and function "int CountVowels (IN text[], OUT Count)". the OUT parameter must be of type counter_t. I've written the code but it doesn't seem to work. Can someone help me please???

    -------------------------------------------------------------------------------------------------------------
    #include <stdio.h>

    typedef struct {
    int a;
    int e;
    int i;
    int o;
    int u;
    }counter_t;

    int CountVowels (char text[], counter_t Count);

    int main ()
    {
    char text[100];
    counter_t Count;
    Count.a = 0;
    Count.e = 0;
    Count.i = 0;
    Count.o = 0;
    Count.u = 0;
    printf ("Text\n");
    gets(text);
    CountVowels(tex t, Count);
    printf("a = %d\n", Count.a);
    printf("e = %d\n", Count.e);
    printf("i = %d\n", Count.i);
    printf("o = %d\n", Count.o);
    printf("u = %d\n", Count.u);
    fflush (stdin);
    getchar ();
    return 0;
    }

    int CountVowels (char text[], counter_t Count)
    {
    int i;
    for (i = 0; text[i] != '\0', i++)
    {
    switch(text[i])
    {
    case 'a': Count.a++; break;
    case 'e': Count.e++; break;
    case 'i': Count.i++; break;
    case 'o': Count.o++; break;
    case 'u': Count.u++; break;
    }
    }
    return (Count);
    }
    ------------------------------------------------------------------------------------------------------
  • boxfish
    Recognized Expert Contributor
    • Mar 2008
    • 469

    #2
    Hi,
    There are two things that I see wrong offhand. Since CountVowels returns a Count object, its return type has to be Count, not int. Make it:
    Code:
    Count CountVowels (char text[], counter_t Count);
    Also, your CountVowels function does not count uppercase vowels. The tolower function from ctype.h should be useful for that.
    Hope this helps.

    Comment

    • ashpats
      New Member
      • Sep 2008
      • 5

      #3
      well i've improved my code a bit. but it still has some problems. (the program doesn't have to count upper case vowels).

      #include <stdio.h>

      typedef struct {
      int a;
      int e;
      int i;
      int o;
      int u;
      }counter_t;

      counter_t CountVowels (char text[], counter_t* Count);

      int main ()
      {
      char text[100];
      counter_t Count;
      Count.a = 0;
      Count.e = 0;
      Count.i = 0;
      Count.o = 0;
      Count.u = 0;
      printf ("Text\n");
      fgets(text, sizeof text, stdin);
      Count = CountVowels(tex t, Count);
      printf("a = %d\n", Count.a);
      printf("e = %d\n", Count.e);
      printf("i = %d\n", Count.i);
      printf("o = %d\n", Count.o);
      printf("u = %d\n", Count.u);
      getchar ();
      return 0;
      }

      counter_t CountVowels (char text[], counter_t* Count)
      {
      int i;
      for (i = 0; text[i] != '\0'; i++)
      {
      switch(text[i])
      {
      case 'a': Count->a++; break;
      case 'e': Count->e++; break;
      case 'i': Count->i++; break;
      case 'o': Count->o++; break;
      case 'u': Count->u++; break;
      }
      }
      return (Count);
      }

      Comment

      • Laharl
        Recognized Expert Contributor
        • Sep 2007
        • 849

        #4
        What problems are you encountering? We're neither telepathic nor C compilers (either would be kinda cool, just look at code and BOOM! it's compiled), so if you can post what's going wrong, we can help you.

        Also, please surround your code with [CODE] tags. It makes it much easier to read.

        Comment

        • ashpats
          New Member
          • Sep 2008
          • 5

          #5
          [Count = CountVowels(tex t, Count);] error C2440: 'function' : cannot convert from 'counter_t' to 'counter_t *'

          and
          [return (Count);] error C2440: 'return' : cannot convert from 'counter_t *' to 'counter_t'

          Comment

          • gpraghuram
            Recognized Expert Top Contributor
            • Mar 2007
            • 1275

            #6
            Originally posted by ashpats
            [Count = CountVowels(tex t, Count);] error C2440: 'function' : cannot convert from 'counter_t' to 'counter_t *'

            and
            [return (Count);] error C2440: 'return' : cannot convert from 'counter_t *' to 'counter_t'

            This line is the issue.
            Count = CountVowels(tex t, Count);
            Pass the Count by its address like
            Count = CountVowels(tex t, &Count);


            Raghu

            Comment

            • newb16
              Contributor
              • Jul 2008
              • 687

              #7
              Originally posted by boxfish
              Hi,
              There are two things that I see wrong offhand. Since CountVowels returns a Count object, its return type has to be Count, not int.
              No. Assignment specification reads "int vowelcount(IN text, OUT count);"
              So second arg needs to be count* .

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                If you are tassing a pointer to counter_t into the function CountVowels that is the location of the structure to store the results in (and therefore provides a channel to pass the results back to the calling code) then ask yourself what is the purpose of the return value of the function CountVowels, what could it return, what does it need to return?

                From that decide what type it should be and change your function accordingly.

                Comment

                • ashpats
                  New Member
                  • Sep 2008
                  • 5

                  #9
                  well i've changed few lines:

                  Count = CountVowels(tex t, Count); to Count = CountVowels(tex t, Count*);

                  counter_t CountVowels (char text[], counter_t* Count) to
                  void CountVowels (char text[], counter_t* Count)

                  end got rid of 'return'. but now i get a syntax error:
                  Count = CountVowels(tex t, Count*); error C2059: syntax error : ')'

                  Comment

                  • Laharl
                    Recognized Expert Contributor
                    • Sep 2007
                    • 849

                    #10
                    If you don't return anything from the function, you can't set its return value to a variable, now can you?

                    Comment

                    • boxfish
                      Recognized Expert Contributor
                      • Mar 2008
                      • 469

                      #11
                      I see that you were right that the function was supposed to return an int. Sorry about that. I think the Count parameter is supposed to be passed by reference so you can modify it in the function but not return it, and the int the function returns is the total number of vowels; the sum of all the variables in the struct. So the prototype would be:
                      Code:
                      int CountVowels (char text[], counter_t* Count);
                      And at the end of the function, you add up all the individual vowel counts and return that.

                      Here's a link to a reference for the tolower function.

                      Comment

                      • curiously enough
                        New Member
                        • Aug 2008
                        • 79

                        #12
                        You don't need to get rid of return, just write return (*Count); instead of return(Count); so not to return an adress.

                        Comment

                        • ashpats
                          New Member
                          • Sep 2008
                          • 5

                          #13
                          thanx guys. i finally finished it.
                          the line
                          Count = CountVowels(tex t, Count);
                          was the problem. i simply changed it to
                          CountVowels(tex t, &Count);
                          and it appears to be workin'

                          Comment

                          Working...