Tuesday, January 24, 2012

Sample C Program To Print Number Of Vowels, Consonants, Characters, Words & Spaces In A Line Of Text.

#include
#include
#include
main()
{

clrscr();
char line[80], c;
int i, vow, cons, dig, word, whites, other;

i = 0;
vow = 0;
cons = 0;
dig = 0;
word = 0;
whites = 0;
other = 0;

printf ( " Enter a line of text: \n" );
scanf ( " % [ ^ \n ] ", line);
while ( ( c = tolower ( line [ i++ ] ) ) ! = '\0' )
{
if ( c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' )
++vow;

else if ( c >= 'a' && c <= 'z' )
++cons;

else if ( c >= '0' && c <= '9' )
++dig;

else if ( c == ' ' )
{
++word;
++whites;
while ( ( line[i] == ' ' || line[i] == '\t' ) )
{
i++;
whites++;
}
}

else
++other;

}

++word;
printf ( " \n\n Total number of :\n " );
printf( " Vowels = %d\n ", vow );
printf( " Consonants = %d\n ", cons );
printf( " Numeric digits = %d\n ", dig );
printf( " Other characters = %d\n ", other );
printf( " Words = %d\n ", word );
printf( " White spaces = %d\n ", whites );
return 0;

}

No comments:

Post a Comment