C program to count the number of vowels, consonants, spaces and digits in a sentances - wordsclank.in

Tuesday 5 June 2018

C program to count the number of vowels, consonants, spaces and digits in a sentances

Vowels, consonant, C program

In this program we will count the number of vowels, consonants, digits or blank space present in a sentence given by user as input.



Program related to string:


Problem Statement:

C program to count the number of vowels, consonants, spaces and digits in a sentence.

Logic :


  • The program  took a string sentence from the user.
  • At first, the variables vowels, consonants, number and spaces are initialized to 0
  • Within a  for loop  we check for  vowels, consonants, number and spaces and when found we increment the corresponding variable by 1.
  • At last, we are printing the corresponding count on the screen. 





Source Code:


1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include
#include
#include

#define SIZE 100 //size of the string

void main()
{
 char sentence[SIZE],ch;
 int i;
 int vowels=0,consonants=0,number=0,spaces=0;
 printf("Enter the sentance \n");
 gets(sentence);
 for(i=0; i<strlen(sentence); i++)
 {
  if(sentence[i]=='A' || sentence[i]=='a' || sentence[i]=='E' || sentence[i]=='e' || 
        sentence[i]=='I' || sentence[i]=='i' || sentence[i]=='O' || sentence[i]=='o' || 
     sentence[i]=='U' || sentence[i]=='u')
        {
            vowels++;
        }
        else if((sentence[i]>='a' && sentence[i]<='z') || (sentence[i]>='A && sentence[i]<='Z'))
        {
            consonants++;
        }
        else if(sentence[i]>='0' && sentence[i]<='9')
        {
            number++;
        }
        else if (sentence[i]==' ')
        {
            spaces++;
        }
    }
    printf("The number of vowel is %d ,consonant is %d, number is %d and blank spaces is %d \n",vowels,consonants,number,spaces);
    
}

Output:


vowels,consonants in c programing


More topics on C programs:

No comments:

Post a Comment