This is the continuation of character type array. Here is another C program on string.
Take a sentence from the user and count the number of vowels, consonants, punctuations, and words in the sentence.
#include< stdio.h >
#include< string.h >
void main()
{
char ch [50];
int i,vowel=0,cons=0,space=0,punc=0,word=0;
clrscr();
puts("Enter the string:-");
gets(ch);
for (i=0;i< strlen (ch);i++)
{
if((ch[i]=='a')||(ch[i]=='e')||(ch[i]=='i')||(ch[i]=='o')||
(ch[i]=='u')||(ch[i]=='A')||(ch[i]=='E')||(ch[i]=='I')||
(ch[i]=='O')||(ch[i]=='U'))
vowel++;
else if((ch[i]==',')||(ch[i]=='.')||(ch[i]==';')||(ch[i]==':')
||(ch[i]=='"')||(ch[i]=='?'))
{
punc++;
word++;
}
else if(ch[i]==' ')
{
space++;
word++;
}
else
cons++;
}
printf("Total vowels in the string is :-%d.",vowel);
printf("\nTotal consonants in the string is :-%d.",cons);
printf("\nTotal spaces in the string is :-%d.",space);
printf("\nTotal words in the string is :-%d.",word);
printf("\nTotal punctuations in the string is :-%d.",punc);
getch();
}
No comments:
Post a Comment