Write a C program to calculate number of characters, vowels, consonants, words and white space in a string.
#include <stdio.h>
#include <string.h>
int main() {
char string[256];
char vowels[10] = {'a', 'e', 'i', 'o', 'u',
'A', 'E', 'I', 'O', 'U'};
int word, vowel, cons, space, character, i, j, flag;
word = vowel = cons = space = character = i = j = 0;
/* get the input string from the user */
printf("Enter your input string:");
fgets(string, 256, stdin);
string[strlen(string) - 1] = '\0';
/* in case user input is only one world */
if (string[i] != '\0')
word++;
/* count the number of characters, vowel, consonants and words */
while (string[i] != '\0') {
flag = 0;
/* calculates number of white space and word */
if (string[i] == ' ') {
space++;
word++;
i++;
continue;
}
#include <string.h>
int main() {
char string[256];
char vowels[10] = {'a', 'e', 'i', 'o', 'u',
'A', 'E', 'I', 'O', 'U'};
int word, vowel, cons, space, character, i, j, flag;
word = vowel = cons = space = character = i = j = 0;
/* get the input string from the user */
printf("Enter your input string:");
fgets(string, 256, stdin);
string[strlen(string) - 1] = '\0';
/* in case user input is only one world */
if (string[i] != '\0')
word++;
/* count the number of characters, vowel, consonants and words */
while (string[i] != '\0') {
flag = 0;
/* calculates number of white space and word */
if (string[i] == ' ') {
space++;
word++;
i++;
continue;
}
/* calculates number of vowels */
for (j = 0; j < 10; j++) {
if (string[i] == vowels[j]) {
vowel++;
flag = 1;
break;
}
}
/* calculates number of consonants */
if (!flag) {
if (string[i] >= 'A' || string[i] <= 'Z' ||
string[i] >= 'a' || string[i] <= 'z') {
cons++;
}
}
/* number of alpha characters */
character++;
i++;
}
/* print the result */
printf("Number of words: %d\n", word);
printf("Number of white spaces: %d\n", space);
printf("Number of characters: %d\n", character);
printf("Number of consonants: %d\n", cons);
printf("Number of vowels: %d\n", vowel);
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Enter your input string:India is my country
Number of words: 4
Number of white spaces: 3
Number of characters: 16
Number of consonants: 10
Number of vowels: 6
Enter your input string:India is my country
Number of words: 4
Number of white spaces: 3
Number of characters: 16
Number of consonants: 10
Number of vowels: 6
No comments:
Post a Comment