Link to home
Start Free TrialLog in
Avatar of slracer99
slracer99

asked on

Count the number of vowels

I'am trying to count the number of vowels read from a redirected file. The problem is I can't seem to create the counter and send back the number of vowels read.
Any advice is greatly appreciated.
ASKER CERTIFIED SOLUTION
Avatar of guitardude101
guitardude101

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of guitardude101
guitardude101

FYI you need to include <stdio,h> and <ctype.h>
what do u mean by
>problem is I can't seem to create the counter and send back the >number of vowels read

u may have to do dis

count = 0;
while  not end of file
{
   read the file contents in a buffer
   check if the buffer has a vowel
   if so increment the count
}
u may need to use sum of these
fopen
rewind/fseek //optional
fgets/fread
feof
fclose
Just a quick comment to guitardude101's post - cover all cases:

              switch (ch)
              {
                   case 'A':
                   case 'A':
                   case 'E':
                   case 'e':
                   case 'I':
                   case 'i':
                   case 'O':
                   case 'o':
                   case 'U':
                   case 'u':
                        count++;
                        break;
                       
                    default:
                        break;
 
mrdtn if you look carefully you will see that I did a toupper(ch) thus there is no need to have lowwer case volowels in the switch statement.


slracer99 please close and grade. You have your working answer.
>> Just a quick comment  . . .

Perhaps a bit too quick.

Didn't see the toupper.  Never mind my comment.

mrdtn
How is your file to be terminated?
Herre's something that works generically.
If you have a tradational 'end-of-file' marker, you can change the validation to look for character 0x1a.

execution will look like this

ProgName < this.txt

...where this.txt is a file of text.

#include <stdio.h>
#include <ctype.h>

int main(void)
{
      auto      long      lngCount=0;
      auto      char      chrData=0;

      while(0x09 < (chrData=(char)toupper(getchar())))
            {
            if((chrData=='A') || (chrData=='E') || (chrData=='I') || (chrData=='O') || (chrData=='U'))
                  {
                  lngCount++;
                  }
            }

      printf("Number of vowels=%ld", lngCount);
}
/*
Here's an example using stdin
*/

#include <stdio.h>
#include <ctype.h>

int main(void)
{
      auto      FILE*      hanInFile=stdin;
      auto      char      chrData=0;
      auto      long      lngCount=0;

      if(NULL == hanInFile)
            {
            printf("cannot open input file");
            return 1;
            }

      while (!feof(hanInFile))
            {
            chrData = (char)toupper(fgetc(hanInFile));
            if((chrData=='A') || (chrData=='E') || (chrData=='I') || (chrData=='O') || (chrData=='U'))
                  {
                  lngCount++;
                  }
            }
      fclose(hanInFile);
      printf("Number of vowels=%ld", lngCount);
      return 0;
}
My original and first answer is correct and works.
The solution using stdin also works.

Have a good day
Paul