|
|
| C Tutorial
The following C program explains about
character handling in c
Sample Program 2
|
|
To count the no:of vowels,consonants and
space in a line vowels:a,e,i,o,u consonants:everything else
|
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#define NTYPES 4
enum CHAR_TYPE { NONE,VOWEL,CONSONANT,SPACE };
char vowels[]="aeiouAEIOU";
enum CHAR_TYPE get_type(char c)
{
enum CHAR_TYPE retval = NONE;
if(c==' ')
retval=SPACE;
else if(isalpha(c))
{
if(strchr(vowels,c))
retval=VOWEL;
else
retval=CONSONANT;
}
return retval;
}
void main()
{
char input[80];
int len , i;
int counts[NTYPES]={0,0,0,0};
puts("Enter a line of text");
gets(input);
len=strlen(input);
for(i=0;i<len;i++)
{
int index=get_type(input[i]);
counts[index]++;
}
printf("results:\n");
printf("vowels:%d\n",counts[VOWEL]);
printf("consonants:\n%d",counts[CONSONANTS]);
printf("Space:%d\n",counts[SPACE]);
}
The %d
is a format specifier which specifies the data type of the variable
defined .here %d is for an integer variable. Array variable is of the
following form array_name[index] array_name is the array variable's
name, and index is the subscript or the size of the array while it is
defined and for the lformer case it is the index value. The
isalpha(arg) function defined in ctype.h file returns true if the
argument is an alphabet or else returns false or 0.
|
|
|