Solved

Count the number of vowels

Posted on 2003-10-27
12
279 Views
Last Modified: 2012-05-04
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.
0
Comment
Question by:slracer99
  • 4
  • 2
  • 2
  • +1
12 Comments
 
LVL 1

Accepted Solution

by:
guitardude101 earned 50 total points
ID: 9631310
Here is my straight foward solution

1) open the file in buffered mode
2) read a char and convert to upper case
3) use a switch statement to compare the char to A E I O U
4) return count

int count_vowels(char* filename)
{
      FILE * thefile;
      int count = 0;
      char ch;

      thefile  = fopen(filename,  "r");
      if (thefile)
      {
            while (!feof(thefile))
            {
                  ch = fgetc(thefile);
                  ch = toupper(ch);
                  switch (ch)
                  {
                        case 'A':
                        case 'E':
                        case 'I':
                        case 'O':
                        case 'U':
                              count++;
                              break;
                              
                        default:
                              break;
                  }
            }
            fclose(thefile);
      }
      
      return count;
}
0
 
LVL 1

Expert Comment

by:guitardude101
ID: 9631313
FYI you need to include <stdio,h> and <ctype.h>
0
 

Expert Comment

by:not_an_xpert
ID: 9631314
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
0
 
LVL 6

Expert Comment

by:mrdtn
ID: 9654424
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;
 
0
The problems with reply email signatures

Do you wish that you could place an email signature under a reply? Well, unfortunately, you can't. That great Exchange/Office 365 signature you've created will just appear at the bottom of an email chain. What a pain! Is there really no way to solve this? Well, there might be...

 
LVL 1

Expert Comment

by:guitardude101
ID: 9655955
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.
0
 
LVL 6

Expert Comment

by:mrdtn
ID: 9656854
>> Just a quick comment  . . .

Perhaps a bit too quick.

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

mrdtn
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 9663808
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);
}
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 9663877
/*
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;
}
0
 
LVL 1

Expert Comment

by:guitardude101
ID: 10413067
My original and first answer is correct and works.
The solution using stdin also works.

Have a good day
Paul
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now