Problem statement:- Program to Count the number of vowels, consonants, numbers, and special characters present in a string.
Data requirement:-
Input Data:- str
Output Data:- count
Output Data:- count
Additional Data:- i, flag
Program in C
Here is the source code of the C Program to Count the number of vowels, consonants, numbers, and special characters 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 v_count=0,s_count=0,n_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')
{
v_count++;
}
else if(str[i]>='0' && str[i]<='9')
n_count++;
else if(str[i]>= 0 && str[i]<=47 || str[i]>= 58 && str[i]<=64 || str[i]>=91 && str[i]<=96 || str[i]>= 123 && str[i]<=127)
s_count++;
}
printf("Number of digits: %d\n",n_count);
printf("Number of vowels: %d\n",v_count);
printf("Number of special character: %d\n",s_count);
printf("Number of consonants: %d\n",strlen(str)-n_count-v_count-s_count);
}
#include<string.h>
int main()
{
char str[30];
printf("Enter your String:");
scanf("%[^\n]",str);
int i;
int v_count=0,s_count=0,n_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')
{
v_count++;
}
else if(str[i]>='0' && str[i]<='9')
n_count++;
else if(str[i]>= 0 && str[i]<=47 || str[i]>= 58 && str[i]<=64 || str[i]>=91 && str[i]<=96 || str[i]>= 123 && str[i]<=127)
s_count++;
}
printf("Number of digits: %d\n",n_count);
printf("Number of vowels: %d\n",v_count);
printf("Number of special character: %d\n",s_count);
printf("Number of consonants: %d\n",strlen(str)-n_count-v_count-s_count);
}
Input/Output:
Enter your String:csinfo360.com
Number of digits: 3
Number of vowels: 3
Number of special character: 1
Number of consonants: 6
Number of digits: 3
Number of vowels: 3
Number of special character: 1
Number of consonants: 6
Program in C++
Here is the source code of the C++ Program to Count the number of vowels, consonants, numbers, and special characters present in a string.
Code:
#include<iostream>
using namespace std;
int main()
{
string str;
cout<<"Enter your String:";
getline(cin,str);
int len=0;
while(str[len]!='\0')
{
len++;
}
int i;
int v_count=0,s_count=0,n_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')
{
v_count++;
}
else if(str[i]>='0' && str[i]<='9')
n_count++;
else if(str[i]>= 0 && str[i]<=47 || str[i]>= 58 && str[i]<=64 || str[i]>=91 && str[i]<=96 || str[i]>= 123 && str[i]<=127)
s_count++;
}
cout<<"Number of digits: "<<n_count<<"\n";
cout<<"Number of vowels: "<<v_count<<"\n";
cout<<"Number of special character: "<<s_count<<"\n";
cout<<"Number of consonants: "<<len-n_count-v_count-s_count<<"\n";
}
using namespace std;
int main()
{
string str;
cout<<"Enter your String:";
getline(cin,str);
int len=0;
while(str[len]!='\0')
{
len++;
}
int i;
int v_count=0,s_count=0,n_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')
{
v_count++;
}
else if(str[i]>='0' && str[i]<='9')
n_count++;
else if(str[i]>= 0 && str[i]<=47 || str[i]>= 58 && str[i]<=64 || str[i]>=91 && str[i]<=96 || str[i]>= 123 && str[i]<=127)
s_count++;
}
cout<<"Number of digits: "<<n_count<<"\n";
cout<<"Number of vowels: "<<v_count<<"\n";
cout<<"Number of special character: "<<s_count<<"\n";
cout<<"Number of consonants: "<<len-n_count-v_count-s_count<<"\n";
}
Enter your String:c ++ string 18 program
Number of digits: 2
Number of vowels: 3
Number of special character: 6
Number of consonants: 11
Number of digits: 2
Number of vowels: 3
Number of special character: 6
Number of consonants: 11
Program in Java
Here is the source code of the Java Program to Count the number of vowels, consonants, numbers, and special characters present in a string.
Code:
import java.util.Scanner;
public class p20 {
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
String str1;
System.out.println("Enter your String:");
str1=cs.nextLine();
char[] str=str1.toCharArray();
int i;
int v_count=0,s_count=0,n_count=0;
for(i=0;i<str.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')
{
v_count++;
}
else if(str[i]>='0' && str[i]<='9')
n_count++;
else if(str[i]>= 0 && str[i]<=47 || str[i]>= 58 && str[i]<=64 || str[i]>=91 && str[i]<=96 || str[i]>= 123 && str[i]<=127)
s_count++;
}
System.out.println("Number of digits: "+n_count);
System.out.println("Number of vowels: "+v_count);
System.out.println("Number of special character: "+s_count);
System.out.println("Number of consonants: "+(str.length-n_count-v_count-s_count));
cs.close();
}
}
public class p20 {
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
String str1;
System.out.println("Enter your String:");
str1=cs.nextLine();
char[] str=str1.toCharArray();
int i;
int v_count=0,s_count=0,n_count=0;
for(i=0;i<str.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')
{
v_count++;
}
else if(str[i]>='0' && str[i]<='9')
n_count++;
else if(str[i]>= 0 && str[i]<=47 || str[i]>= 58 && str[i]<=64 || str[i]>=91 && str[i]<=96 || str[i]>= 123 && str[i]<=127)
s_count++;
}
System.out.println("Number of digits: "+n_count);
System.out.println("Number of vowels: "+v_count);
System.out.println("Number of special character: "+s_count);
System.out.println("Number of consonants: "+(str.length-n_count-v_count-s_count));
cs.close();
}
}
Input/Output:
Enter your String:
java5pyhoncsingo360#&&&
Number of digits: 4
Number of vowels: 5
Number of special character: 4
Number of consonants: 10
java5pyhoncsingo360#&&&
Number of digits: 4
Number of vowels: 5
Number of special character: 4
Number of consonants: 10
Program in Python
Here is the source code of the Python Program to Count the number of vowels, consonants, numbers, and special characters present in a string.
Code:
str=input("Enter the String:")
v_count = 0
s_count = 0
n_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':
v_count+=1
elif str[i] >= '0' and str[i] <= '9':
n_count+=1
elif str[i] >=chr(0) and str[i] <= chr(47) or str[i] >= chr(58) and str[i] <=chr(64) or str[i] >=chr(91) and str[i] <= chr(96) or str[i] >= chr(123) and str[i] <= chr(127):
s_count+=1
print("Number of digits: ",n_count)
print("Number of vowels: ", v_count)
print("Number of special character: ",s_count)
print("Number of consonants: ",len(str) - n_count - v_count - s_count)
v_count = 0
s_count = 0
n_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':
v_count+=1
elif str[i] >= '0' and str[i] <= '9':
n_count+=1
elif str[i] >=chr(0) and str[i] <= chr(47) or str[i] >= chr(58) and str[i] <=chr(64) or str[i] >=chr(91) and str[i] <= chr(96) or str[i] >= chr(123) and str[i] <= chr(127):
s_count+=1
print("Number of digits: ",n_count)
print("Number of vowels: ", v_count)
print("Number of special character: ",s_count)
print("Number of consonants: ",len(str) - n_count - v_count - s_count)
Input/Output:
Enter the String:csinfo360.com programming
Number of digits: 3
Number of vowels: 6
Number of special character: 2
Number of consonants: 1
Number of digits: 3
Number of vowels: 6
Number of special character: 2
Number of consonants: 1
0 Comments
Please do not Enter any spam link in the comment box