Write a C program to find the number of vowels, consonants, digits & symbols in a given string using pointers.
#include <stdlib.h>
#include <string.h>
int main() {
char *ptr;
char vowels[] = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};
int vowel = 0, cons = 0, digit = 0;
int flag = 0, punct = 0, symbol = 0, others = 0, i;
ptr = (char *)malloc(sizeof (char) * 100);
/* get the input string from user */
printf("Enter your input:");
fgets(ptr, 100, stdin);
ptr[strlen(ptr) - 1] = '\0';
/* parse the string to find the no of vowels, digits etc */
while (*ptr != '\0') {
for (i = 0; i < 10; i++)
if (*ptr == vowels[i]) {
flag = 1;
break;
}
if (flag) {
/* no of vowels */
vowel++;
flag = 0;
} else if ((*ptr >= 'A' && *ptr <= 'Z') ||
(*ptr >= 'a' && *ptr <= 'z')) {
/* no of consonants */
cons++;
} else if (*ptr >= '0' && *ptr <= '9') {
/* no of digits */
digit++;
} else if (*ptr == '+' || *ptr == '-' || *ptr == '/'
|| *ptr == '*' || *ptr == '%') {
/* no of symbols */
symbol++;
} else {
others++;
}
ptr++;
}
printf("No of vowels: %d\n", vowel);
printf("No of consonants: %d\n", cons);
printf("No of digits: %d\n", digit);
printf("No of symbols: %d\n", symbol);
printf("Others: %d\n", others);
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Enter your input:1=>see-programming.blogspot.com
No of vowels: 8
No of consonants: 17
No of digits: 1
No of symbols: 1
Others: 4
Enter your input:1=>see-programming.blogspot.com
No of vowels: 8
No of consonants: 17
No of digits: 1
No of symbols: 1
Others: 4
No comments:
Post a Comment