I am learning C so I am doing different C programming challenges one of which is this program. WAP to accept a string input from the user and print the no. vowels and the most repeated vowel in the string.
I am able to calculate and print the number of vowels but I am not able to figure out how should I calculate the most repeated vowel in the string though I can print out directly on the output screen, the most repeated vowel just by having a print statement of vowels[i]
inside the inner loop. But I want to calculate the most repeated vowel inside the program itself and then just print it efficiently. I tried different things but nothing worked. Also, I want to keep the code small and efficient. Is there a way to that?
This is what I did so far:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX 150
int main()
{
char Statement[MAX];
char vowels[] = {'a', 'e', 'i', 'o', 'u'};
int i, j;
puts("Enter a simple statement.");
fgets(Statement, 150, stdin);
printf("\nYou entered: %s\n", Statement);
for(i=0; i<=strlen(Statement); i++){
//Converting uppercase input to lowercase
Statement[i] = tolower((unsigned char)Statement[i]);
}
//Calling the function to print no. of
NumberOfVowels(Statement, vowels); vowels
return 0;
}
int NumberOfVowels(char Statement[], char vowels[])
{
int i, j, vowelsAmount = 0, count = 0;
for(i=0; i<5; i++){ //outer loop
for(j=0; j<=strlen(Statement); j++){ //inner loop
if(vowels[i] == Statement[j]){
vowelsAmount++;
printf("%c - ", vowels[i]);
}
}
}
printf("\nTotal number of vowels = %d", vowelsAmount);
return vowelsAmount;
}
You can try any of the below approach ...
MAP
with key
and value
as members. Have helper methods that allow you to populate MAP
struct with key
as vowel
and value as no. of occurrences
.vowels
and column no. of occurrences
.ascii
) and keep on increment the value at array[ascii(vowel)]
by 1 on each occurrence.Here is one of the implementation (I have just updated your code so its crude, please work on making it look good and remove redundant prints)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX 150
int NumberOfVowels(char Statement[], char vowels[], int counts[])
{
int i, j = 0;
for(j=0; j<strlen(Statement); j++) {
if (Statement[j] == '\0' || Statement[j] == '\n') {
continue;
}
for(i=0; i<5; i++) {
if(vowels[i] == Statement[j]) {
int ascii = (int) vowels[i];
counts[ascii] = counts[ascii] + 1;
}
}
}
for(i=0; i<5; i++) {
int ascii = (int) vowels[i];
printf("%c - %d ", vowels[i], counts[ascii]);
}
printf("\n");
return 0;
}
int main()
{
char Statement[MAX];
char vowels[] = {'a', 'e', 'i', 'o', 'u'};
int counts[256] = {0};
int i, j;
puts("Enter a simple statement.");
fgets(Statement, 150, stdin);
printf("\nYou entered: %s\n", Statement);
for(i=0; i<=strlen(Statement); i++){
//Converting uppercase input to lowercase
Statement[i] = tolower((unsigned char)Statement[i]);
printf("%c ", Statement[i]);
}
//Calling the function to print no. of
NumberOfVowels(Statement, vowels, counts);
return 0;
}
Sample Run
You entered: Hello How are you, I am fine!
h e l l o h o w a r e y o u , i a m f i n e !
a - 2 e - 3 i - 2 o - 3 u - 1