Problem statement:- Program to Print only consonants in a string.
Data requirement:-
Input Data:- str
Output Data:-str
Output Data:-str
Additional Data:- i
Program in C
Here is the source code of the C Program to print only consonants in a string.
Code:
#include<stdio.h>
#include<string.h>
int main()
{
char str[30];
printf("Enter your String:");
scanf("%[^\n]",str);
int i;
printf("All the consonants in the string are:\n");
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
printf("%c ",str[i]);
}
}
#include<string.h>
int main()
{
char str[30];
printf("Enter your String:");
scanf("%[^\n]",str);
int i;
printf("All the consonants in the string are:\n");
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
printf("%c ",str[i]);
}
}
Input/Output:
Enter your String:C String program
All the consonants in the string are:
C S t r n g p r g r m
All the consonants in the string are:
C S t r n g p r g r m
Program in C++
Here is the source code of the C++ Program to print only consonants 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++;
}
cout<<"All the consonants in the string are:\n";
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
cout<<str[i]<<" ";
}
}
#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++;
}
cout<<"All the consonants in the string are:\n";
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
cout<<str[i]<<" ";
}
}
Input/Output:
Enter your String:c plus plus
All the consonants in the string are:
c p l s p l s
All the consonants in the string are:
c p l s p l s
Program in Java
Here is the source code of the Java Program to print only consonants in a string.
Code:
import java.util.Scanner;
public class p15 {
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();
System.out.print("All the consonants in the string are:");
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
System.out.print(str[i]+" ");
}
cs.close();
}
}
public class p15 {
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();
System.out.print("All the consonants in the string are:");
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
System.out.print(str[i]+" ");
}
cs.close();
}
}
Enter your String:
csinfo
All the consonants in the string are:c s n f
csinfo
All the consonants in the string are:c s n f
Program in Python
Here is the source code of the Python Program to print only consonants in a string.
Code:
str=input("Enter the String:")
print("All the consonants in the string are:")
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':
continue
else:
print(str[i],end=" ")
print("All the consonants in the string are:")
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':
continue
else:
print(str[i],end=" ")
Enter the String:python program
All the consonants in the string are:
p y t h n p r g r m
All the consonants in the string are:
p y t h n p r g r m
0 Comments
Please do not Enter any spam link in the comment box