letter countWrite a C program that will analyze a line of text and will print the number of words, the number of consonants, and the number of vowels in the text.Testcase:Input:Programming is fun!Output:3 // Number of words:11 // Number of consonants:5 // Number of vowels:
Question
letter countWrite a C program that will analyze a line of text and will print the number of words, the number of consonants, and the number of vowels in the text.Testcase:Input:Programming is fun!Output:3 // Number of words:11 // Number of consonants:5 // Number of vowels:
Solution
Here is a simple C program that will analyze a line of text and print the number of words, the number of consonants, and the number of vowels in the text.
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main() {
char str[100];
int i, vowels = 0, consonants = 0, words = 0;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
for(i=0; str[i]!='\0'; ++i) {
if(str[i]==' ' || str[i]=='\n' || str[i]=='\t') {
words++;
}
else if((str[i]>=65 && str[i]<=90) || (str[i]>=97 && str[i]<=122)) {
switch(tolower(str[i])) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
vowels++;
break;
default:
consonants++;
}
}
}
printf("Number of words: %d\n", words);
printf("Number of vowels: %d\n", vowels);
printf("Number of consonants: %d\n", consonants);
return 0;
}
This program works by iterating over each character in the input string. If the character is a space, newline, or tab, it increments the word count. If the character is a letter, it checks whether it's a vowel or a consonant by converting it to lowercase and using a switch statement. If it's a vowel, it increments the vowel count; otherwise, it increments the consonant count.
Similar Questions
Write a program in C to count the number of vowels and consonants in a string. You can use fgets to read the characters.
Q) Write a program to input a string and convert it into uppercase and print the pair of vowels and number of pair of vowels occurring in the string. Example: Input: "BEAUTIFUL BEAUTIES " Output : Pair of vowels: EA, AU, EA, AU, IE No. of pair of vowels: 5
Write a C program to check whether an alphabet is vowel or consonant using switch case
Read a string with words separated by space and return its length and count of consonants.Sample Inputwrite a programSample Output158
find the number of alphabet vowels and words in input using string
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.