C Program to count number of words,vowels, digits and spaces in a given string.

 


#include<stdio.h>

#include<string.h>

main()

{

char str[50];

int i,n,words=0,vowels=0,spaces=0,digits=0;

clrscr();

printf("\nEnter a string:\n ");

gets(str);

strlwr(str);

n=strlen(str);

for(i=0;i<n;i++)

{

if(str[i]==' ')

{

spaces++;

words++;

}

if(str[i]>='0'&&str[i]<='9')

digits++;

if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u')

vowels++;

}

printf("\nNumber of words= %d ",words);

printf("\nNumber of digits= %d",digits);

printf("\nNumber of Vowels= %d",vowels);

printf("\nNumber of Spaces= %d",spaces);

getch();

}



Output:

  1. Enter a string:

  St Philomena College Puttur 574202

  Number of words= 3

  Number of digits= 6

  Number of Vowels= 9

  Number of Spaces= 3


  1. Enter a string:

I went to college

Number of words= 3

Number of digits= 0

Number of Vowels= 6

Number of Spaces= 3



Post a Comment

Previous Post Next Post