Count how many vowels present in a string

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

Data requirement:-

   Input Data:- str

  Output Data:-arr, count

  Additional Data:- i

Program in C

Here is the source code of the C Program to Count how many vowels 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 arr[122]={0};
    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')
    {
        arr[(int)str[i]]++;
        count++;

    }
    }
    if(count==0)
        printf("No vowels are present in the string.");
    else
    {
        printf("Numbers of vowels present in the string are %d\n",count);
    for(i=0;i<122;i++){
        if(arr[i]!=0)
        printf("%c: %d\n",i,arr[i]);
    }}
}

Input/Output:
Enter your String:csinfo dot com
Numbers of vowels present in the string are 4
i: 1
o: 3

Program in C++

Here is the source code of the C++ Program to Count how many vowels 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 arr[122]={0};
    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')
    {
        arr[(int)str[i]]++;
        count++;
    }
    }
    if(count==0)
        cout<<"No vowels are present in the string.";
    else
    {
        cout<<"Numbers of vowels present in the string are "<<count<<"\n";
    for(i=0;i<122;i++){
        if(arr[i]!=0)
        cout<<char(i)<<" : "<<arr[i]<<"\n";
    }}
}

Input/Output:
Enter your String:c plus plus string programing
Numbers of vowels present in the string are 6
a : 1
i : 2
o : 1
u : 2

Program in Java

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

Code:

import java.util.Scanner;
public class p13 {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
String str1;
int arr[]=new int[122];
    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')
    {
    arr[(int)str[i]]++;
        count++;
    }
}
    if(count==0)
    System.out.print("No vowels are present in the string.");
    else
    {
    System.out.println("Numbers of vowels present in the string are "+count);
    for(int i=0;i<122;i++){
        if(arr[i]!=0)
        System.out.println((char)i+":"+arr[i]);
    }}
    cs.close();
}
}

Input/Output:
Enter your String:
string java program'
Numbers of vowels present in the string are 5
a:3
i:1
o:1

Program in Python

Here is the source code of the C Program to Count how many vowels 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':
        count+=1
if count==0:
        print("No vowels are present in the string.")
else:
    print("Numbers of vowels present in the string are ",count)

Input/Output:
Enter the String:python string problem
Numbers of vowels present in the string are  4

Post a Comment

0 Comments