Write a program to create a file and write some contents in it. Then read the file and count and print total number of vowels in it.
Answers
❤❤ this is your answer❤❤
❤❤HOPE IT'S HELPS YOU❤❤
❤❤PLEASE FOLLOW ME ALSO...AND MARK AS BRAIN LIST ❤❤
Answer:
In this example, the number of vowels, consonants, digits, and white-spaces in a string entered by the user is counted.
To understand this example, you should have the knowledge of the following C programming topics:
C Arrays
C Programming Strings
Program to count vowels, consonants etc.
#include <stdio.h>
int main() {
char line[150];
int vowels, consonant, digit, space;
vowels = consonant = digit = space = 0;
printf("Enter a line of string: ");
fgets(line, sizeof(line), stdin);
for (int i = 0; line[i] != '\0'; ++i) {
if (line[i] == 'a' || line[i] == 'e' || line[i] == 'i' ||
line[i] == 'o' || line[i] == 'u' || line[i] == 'A' ||
line[i] == 'E' || line[i] == 'I' || line[i] == 'O' ||
line[i] == 'U') {
++vowels;
} else if ((line[i] >= 'a' && line[i] <= 'z') || (line[i] >= 'A' && line[i] <= 'Z')) {
++consonant;
} else if (line[i] >= '0' && line[i] <= '9') {
++digit;
} else if (line[i] == ' ') {
++space;
}
}
printf("Vowels: %d", vowels);
printf("\nConsonants: %d", consonant);
printf("\nDigits: %d", digit);
printf("\nWhite spaces: %d", space);
return 0;
}
Output
Enter a line of string: adfslkj34 34lkj343 34lk
Vowels: 1
Consonants: 11
Digits: 9
White spaces: 2
Here, the string entered by the user is stored in the line variable.
Initially, the variables vowel, consonant, digit, and space are initialized to 0.
Then, a for loop is used to iterate over characters of a string. In each iteration, whether the character is vowel, consonant, digit, and space is checked. Suppose, the character is a vowel, in this case, the vowel variable is increased by 1.
When the loop ends, the number of vowels, consonants, digits and white spaces are stored in variables vowel, consonant, digit and space respectively.
Explanation: