Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
Program to count number of vowels, consonants, spaces in a sentence in C++

C Program to count number of vowels, consonants and spaces in a sentence

Posted on April 21, 2022

Table of Contents

  • C Program to count number of vowels, consonants and spaces in a sentence
    • Code to count total number of Vowels, consonants and spaces
      • Code to count number of Vowels, consonants and spaces in given sentence, using for loop   – #1
      • Code to count number of Vowel, consonant and spaces in given sentence, using while loop   – #1
      • Code to count number of Vowel, consonant and spaces in given sentence, using do-while loop   – #3

C Program to count number of vowels, consonants and spaces in a sentence

In this article, we will discuss the concept of C Program to count number of vowels, consonants and spaces in a sentence

In this post, we are going to learn how to write a program to count the total number of Vowels , consonants and spaces in the given sentence entered by user using loops in C language

Code to count total number of Vowels, consonants and spaces

Code to count number of Vowels, consonants and spaces in given sentence, using for loop   – #1

In this code, we are going to learn how to count  the total number of  Vowels, consonants and spaces in the given sentence using for loop in C language

Program 1

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char str[100];//declare character array
    int i,vowCount=0,consCount=0,spaces=0;//Declare integer variables and initialize to 0
    printf("Enter a string for count vowels, consonant and spaces\n");
    //Ask string input for count total number vowels and consonant
    gets(str);//reading the input string
    for(i=0; str[i]; i++){
        if(str[i] == 'a'|| str[i] == 'e'|| str[i] == 'i'|| str[i] == 'o'|| str[i] == 'u'
  ||str[i] == 'A'|| str[i] == 'E'|| str[i] == 'I'|| str[i] == 'O'|| str[i] == 'U' ){
    vowCount++;//count total number of vowels
  }
  else if((str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z' )){
    consCount++;//count total number of consonant
  }
  else if(str[i] = ' '){
    spaces++;//count total number of spaces
  }
}
printf("Number of vowels: %d \n",vowCount);//display total number of vowels
printf("Number of consonant: %d \n",consCount);//display total number of consonant
printf("Number of spaces: %d \n",spaces);//display total number of spaces in a string
getch();
    return 0;
}

When the above code is executed, it produces the following result

Enter a string for count vowels, consonant and spaces
C code geeks
Number of vowels: 4
Number of consonants: 6
Number of spaces: 3

 

Code to count number of Vowel, consonant and spaces in given sentence, using while loop   – #1

In this code, we are going to learn how to count  the total number of  Vowels, consonants and spaces in the given sentence using while loop in C language

Program 2

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char str[100];//declare character array
    int i,vowCount=0,consCount=0,spaces=0;//Declare integer variables and initialize to 0
    printf("Enter a sentence for count vowels, consonant and spaces\n");
    //Ask string input for count total number vowels and consonant
    gets(str);//reading the input string
    i=0;
   while(str[i]){
        if(str[i] == 'a'|| str[i] == 'e'|| str[i] == 'i'|| str[i] == 'o'|| str[i] == 'u'
  ||str[i] == 'A'|| str[i] == 'E'|| str[i] == 'I'|| str[i] == 'O'|| str[i] == 'U' ){
    vowCount++;//count total number of vowels
  }
  else if((str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z' )){
    consCount++;//count total number of consonant
  }
  else if(str[i] = ' '){
    spaces++;//count total number of spaces
  }
   i++;
}
printf("Number of vowels: %d \n",vowCount);//display total number of vowels
printf("Number of consonant: %d \n",consCount);//display total number of consonant
printf("Number of spaces: %d \n",spaces);//display total number of spaces in a string
getch();
    return 0;
}

When the above code is executed, it produces the following result

Enter a string for count vowels, consonant and spaces
C is a best Language
Number of vowels: 7
Number of consonants: 9
Number of spaces: 4

 

Code to count number of Vowel, consonant and spaces in given sentence, using do-while loop   – #3

In this code, we are going to learn how to count  the total number of  Vowels, consonants and spaces in the given sentence using do-while loop in C language

Program 3

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char str[100];//declare character array
    int i,vowCount=0,consCount=0,spaces=0;//Declare integer variables and initialize to 0
    printf("Enter a sentence for count vowels, consonant and spaces\n");
    //Ask string input for count total number vowels and consonant
    gets(str);//reading the input string
    i=0;
  do{
        if(str[i] == 'a'|| str[i] == 'e'|| str[i] == 'i'|| str[i] == 'o'|| str[i] == 'u'
  ||str[i] == 'A'|| str[i] == 'E'|| str[i] == 'I'|| str[i] == 'O'|| str[i] == 'U' ){
    vowCount++;//count total number of vowels
  }
  else if((str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z' )){
    consCount++;//count total number of consonant
  }
  else if(str[i] = ' '){
    spaces++;//count total number of spaces
  }
   i++;
} while(str[i]);
printf("Number of vowels: %d \n",vowCount);//display total number of vowels
printf("Number of consonant: %d \n",consCount);//display total number of consonant
printf("Number of spaces: %d \n",spaces);//display total number of spaces in a string
getch();
    return 0;
}

 

When the above code is executed, it produces the following result

Enter a string for count vowels, consonant and spaces
C programming
Number of vowels: 3
Number of consonants: 9
Number of spaces: 1

 

Suggested post

if else in C language

Nested if in C language

Operator in C language

Function in C language

 

Similar post

Java program to print all alphabets in given range

C program to print all alphabets in given range

C++ program to print all alphabets in given range

 

Java program to print all alphabets using loops

C program to print all alphabets using loops

C++ program to print all alphabets using loops

 

Java program to check whether a character is Alphabet or Not

C++ program to check whether a character is Alphabet or Not

C program to check whether a character is Alphabet or Not

Python program to check whether a character is Alphabet or Not

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes