15,667,422 members
See more:
Write a program to count the vowels and letters from text file. Read text a character at a time until you encounter end-of-data. Then print out the number of occurrences of each of the vowels a, e, i, o and u in the text, the total number of letters, and each of the vowels as an integer percentage of the letter total.

Suggested output format is:
Numbers of characters:
a 3 ; e 2 ; i 0 ; o 1 ; u 0 ; rest 17
Percentages of total:
a 13%; e 8%; i 0%; o 4%; u 0%; rest 73%

What I have tried:

C++
```#include<stdio.h>

int main()
{
char vowels[] = {'a', 'e', 'i', 'o', 'u'};
char input;
int freq[6] = {0},index;
double total = 0;
printf("Entertext and press^Z after completion\n");
scanf("%s",&input);
while( ( input =getchar() ) != EOF )
{
if(input == 67 || input == 97)//A or a
freq[0]++;
else if(input == 69 || input == 101)//E or e
freq[1]++;
else if(input == 73 || input == 105)//I or i
freq[2]++;
else if(input == 79 || input == 111)//O or o
freq[3]++;
else if(input == 85 || input == 117)//U or u
freq[4]++;
else //any other letter
freq[5]++;

total++;
}
total--;//subtract ^Z
freq[5]--;
for(int index = 0; index < 6; index++)
{
if(freq[index] != 0 )
{
if(index == 5)
printf("rest");
else
printf("%d",vowels[index]);
printf("%d",freq[index]);
}
}
printf("\n\nPercentage\n");
for(index = 0; index< 6; index++)
{
if(freq[index] != 0 )
{
if(index == 5)
printf("rest");
else
printf("%d",vowels[index]);
printf("%f",(freq[index]/total)*100);
}
}
}```
Posted
Updated 9-Oct-21 5:58am
v2

## Solution 1

C++
`if(input == 67 || input == 97)//A or a`

If you wish to test characters for their actual value then use character constants, not numbers, especially not decimal numbers.

Using proper constants makes the code immediately clear:
C++
`if(input == 'A' || input == 'a')//A or a`

Also, the following will print the vowels as numbers not characters and misses the space and semi-colon:
C++
```printf("%d",vowels[index]);
printf("%d",freq[index]);```

and can be simplified to:
C++
`printf("%c %d ; ", vowels[index], freq[index]);`

surya teja Oct2021 9-Oct-21 10:26am
can you pls get me expected output
Richard MacCutchan 9-Oct-21 10:32am
You forgot the quotes around the characters as shown in my sample.

## Solution 2

Ascii value of 'A' is 65
C++
`if(input == 67 || input == 97)//A or a`

Using literal char as Richard suggested would have avoided this error.

surya teja Oct2021 9-Oct-21 11:05am
can give me expected output

## Solution 3

int main()
{
char vowels[] = {'a', 'e', 'i', 'o', 'u'};
char input,file[50];
int freq[6] = {0},index;
double total = 0;
FILE *fp;
printf("Filename: ");
scanf("%[^\n]",file);
fp=fopen(file,"r");
printf("Reading the contents of the file [%s]\n",file);
while( ( input =fgetc(fp) ) != EOF )
{
if(input == 'A' || input == 'a')//A or a
freq[0]++;
else if(input == 'E' || input == 'e')//E or e
freq[1]++;
else if(input == 'I' || input == 'i')//I or i
freq[2]++;
else if(input == 'O' || input == 'o')//O or o
freq[3]++;
else if(input == 'U' || input == 'u')//U or u
freq[4]++;
else //any other letter
freq[5]++;

total++;
}
total--;//subtract ^Z
freq[5]--;
for(int index = 0; index < 6; index++)
{
if(freq[index] != 0 )
{
if(index == 5)
printf("rest");
else
printf("%d",vowels[index]);
printf("%d",freq[index]);
}
}
printf("\n\nPercentage\n");
for(index = 0; index< 6; index++)
{
if(freq[index] != 0 )
{
if(index == 5)
printf("rest");
else
printf("%c %d",vowels[index],freq[index]);
}
}
}

Patrice T 9-Oct-21 13:24pm
You should tell if it is your solution or a new question with updated code.
Use Improve question to update your question.

## Solution 4

If you don't care about the case of an input letter then I prefer to change it to upper (or lower) and then compare it with just one set of letters. Here is a sample of that :
C++
```while( ( input = getchar() ) != EOF )
{
input = toupper( input );
if( input == 'A' )
freq[0]++;
else if( input == 'E' )
freq[1]++;
...
}
```
Another option is to use a loop. Here's how that could look.
C++
```char vowels[] = "AEIOU";
int n;
while( ( input = getchar() ) != EOF )
{
input = toupper( input );

// search the list of vowels until the end is found

for( n = 0; vowels[n] != '\0'; ++n )
{
if( input == vowels[n] )
{
++freq[n];
++total;
break;     // break from the searching loop
}
}
if( vowels[n] == '\0' )
{
// handle input that was not a vowel
}
}
```
One nice thing about the loop approach is you can look for vowels or any other type of letter by just changing the list of characters that are searched.