Count how many consonants present in a String

Problem statement:- Program to Count how many consonants present in a string.

Data requirement:-

   Input Data:- str

  Output Data:-count

  Additional Data:- i, len

Program in C

Here is the source code of the C Program to Count how many consonants present in a string.

Code:

#include<stdio.h>
#include<string.h>
int main()
{
    char str[30];
    printf("Enter your String:");
    scanf("%[^\n]",str);
    int i;
    int count=0;
    for(i=0;i<strlen(str);i++)
    {
    if(str[i]=='a' || str[i]=='A' || str[i]=='e'|| str[i]=='E' || str[i]=='i'
       || str[i]=='I' || str[i]=='o' || str[i]=='O' || str[i]=='u' || str[i]=='U'
       || str[i]==' ')
    {
     continue;
    }
    else
        count++;
    }
    if(count==0)
        printf("No consonants are present in the string.");
    else
        printf("Numbers of consonants present in the string are %d\n",count);
}

Input/Output:
Enter your String:c program
Numbers of consonants present in the string are 6

Program in C++

Here is the source code of the C++ Program to Count how many consonants present in a string.

Code:

#include<iostream>
#include<cstring>
using namespace std;
int main()
{
    string str;
    cout<<"Enter your String:";
    getline(cin,str);
    int i=0;
    int len=0;
    while(str[len]!='\0')
    {
        len++;
    }
    int count=0;
    for(i=0;i<len;i++)
    {
    if(str[i]=='a' || str[i]=='A' || str[i]=='e'|| str[i]=='E' || str[i]=='i'
       || str[i]=='I' || str[i]=='o' || str[i]=='O' || str[i]=='u' || str[i]=='U'
       || str[i]==' ')
    {
     continue;
    }
    else
        count++;
    }
    if(count==0)
        cout<<"No consonants are present in the string.";
    else
        cout<<"Numbers of consonants present in the string are "<<count;
}

Input/Output:
Enter your String:plus plus c
Numbers of consonants present in the string are 7

Program in Java

Here is the source code of the Java Program to Count how many consonants present in a string.

Code:

import java.util.Scanner;
public class p16 {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
String str1;
int count=0;
System.out.println("Enter your String:");
str1=cs.nextLine();
char[] str=str1.toCharArray();
    for(int i=0;i<str1.length();i++)
    {
    if(str[i]=='a' || str[i]=='A' || str[i]=='e'|| str[i]=='E' || str[i]=='i'|| 
      str[i]=='I' || str[i]=='o' || str[i]=='O' || str[i]=='u' || str[i]=='U'|| str[i]==' ')
    {
    continue;
      }
        else
        count++;
        }
    if(count==0)
    System.out.print("No consonants are present in the string.");
    else
    System.out.print("Numbers of consonants present in the string are "+count);
    cs.close();
}
}

Input/Output:
Enter your String:
csinfo dot com
Numbers of consonants present in the string are 8

Program in Python

Here is the source code of the Python Program to Count how many consonants present in a string.

Code:

str=input("Enter the String:")
count=0
for i in range(len(str)):
    if str[i] == 'a' or str[i] == 'A' or str[i] == 'e' or str[i] == 'E' or str[i] == 'i'or str[i] == 'I' or str[i] == 'o' or str[i] == 'O' or str[i] == 'u' or str[i] == 'U' or str[i]==' ':
        continue
    else:
        count+=1
if count==0:
        print("No consonants are present in the string.")
else:
    print("Numbers of consonants present in the string are ",count)

Input/Output:
Enter the String:python program
Numbers of consonants present in the string are  10

Post a Comment

0 Comments