Here is a program to find the number of consonants in a string in C++. I given 5 different ways you can find the number of consonants in a string like using switch-case, if-else ladder, function, recursion etc.
Method 1 : C++ program to find number of consonants in given string using array
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
char str[20],vowels[]={'a','e','i','o','u','A','E','I','O','U'};
int flag=0,consonants=0;
cout<<"Enter the string ";
gets(str);
for(int i=0;str[i]!='\0';i++)
{
if(str[i]>'z'||str[i]<'A'||(str[i]>'Z'&&str[i]<'a'))
continue;
for(int y=0;y<10;y++)
if(str[i]==vowels[y])
flag=1;
if(flag!=1)
consonants++;
flag=0;
}
cout<<"Number of consonants "<<consonants;
return 0;
}
#include<stdio.h>
using namespace std;
int main()
{
char str[20],vowels[]={'a','e','i','o','u','A','E','I','O','U'};
int flag=0,consonants=0;
cout<<"Enter the string ";
gets(str);
for(int i=0;str[i]!='\0';i++)
{
if(str[i]>'z'||str[i]<'A'||(str[i]>'Z'&&str[i]<'a'))
continue;
for(int y=0;y<10;y++)
if(str[i]==vowels[y])
flag=1;
if(flag!=1)
consonants++;
flag=0;
}
cout<<"Number of consonants "<<consonants;
return 0;
}
Method 2 : C++ program to find number of consonants in given string using if-else ladder
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
char str[20];
int consonant=0;
cout<<"Enter the string ";
gets(str);
for(int i=0;str[i]!='\0';i++)
{
if(str[i]>'z'||str[i]<'A'||(str[i]>'Z'&&str[i]<'a'))
continue;
else if(str[i]=='a'||str[i]=='A')
continue;
else if(str[i]=='e'||str[i]=='E')
continue;
else if(str[i]=='i'||str[i]=='I')
continue;
else if(str[i]=='o'||str[i]=='O')
continue;
else if(str[i]=='u'||str[i]=='U')
continue;
else
consonant++;
}
cout<<"Number of consonant "<<consonant;
return 0;
}
#include<stdio.h>
using namespace std;
int main()
{
char str[20];
int consonant=0;
cout<<"Enter the string ";
gets(str);
for(int i=0;str[i]!='\0';i++)
{
if(str[i]>'z'||str[i]<'A'||(str[i]>'Z'&&str[i]<'a'))
continue;
else if(str[i]=='a'||str[i]=='A')
continue;
else if(str[i]=='e'||str[i]=='E')
continue;
else if(str[i]=='i'||str[i]=='I')
continue;
else if(str[i]=='o'||str[i]=='O')
continue;
else if(str[i]=='u'||str[i]=='U')
continue;
else
consonant++;
}
cout<<"Number of consonant "<<consonant;
return 0;
}
Method 3 : C++ program to find number of consonants in given string using function
#include<iostream>
#include<stdio.h>
using namespace std;
int countc(char *str)
{
int consonant=0;
for(int i=0;str[i]!='\0';i++)
{
if(str[i]>'z'||str[i]<'A'||(str[i]>'Z'&&str[i]<'a'))
continue;
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')
continue;
consonant++;
}
return consonant;
}
int main()
{
char str[20];
int consonant=0;
cout<<"Enter the string ";
gets(str);
consonant=countc(str);
cout<<"Number of consonant "<<consonant;
return 0;
}
#include<stdio.h>
using namespace std;
int countc(char *str)
{
int consonant=0;
for(int i=0;str[i]!='\0';i++)
{
if(str[i]>'z'||str[i]<'A'||(str[i]>'Z'&&str[i]<'a'))
continue;
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')
continue;
consonant++;
}
return consonant;
}
int main()
{
char str[20];
int consonant=0;
cout<<"Enter the string ";
gets(str);
consonant=countc(str);
cout<<"Number of consonant "<<consonant;
return 0;
}
Method 4 : C++ program to find number of consonants in given string using recursion
#include<iostream>
#include<stdio.h>
using namespace std;
int countc(char *str,int consonant,int i)
{
if(str[i]==0)
return consonant;
if(str[i]>'z'||str[i]<'A'||(str[i]>'Z'&&str[i]<'a'));
else 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');
else
consonant++;
countc(str,consonant,i+1);
}
int main()
{
char str[20];
int consonant=0;
cout<<"Enter the string ";
gets(str);
consonant=countc(str,0,0);
cout<<"Number of consonant "<<consonant;
return 0;
}
#include<stdio.h>
using namespace std;
int countc(char *str,int consonant,int i)
{
if(str[i]==0)
return consonant;
if(str[i]>'z'||str[i]<'A'||(str[i]>'Z'&&str[i]<'a'));
else 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');
else
consonant++;
countc(str,consonant,i+1);
}
int main()
{
char str[20];
int consonant=0;
cout<<"Enter the string ";
gets(str);
consonant=countc(str,0,0);
cout<<"Number of consonant "<<consonant;
return 0;
}
Method 5 : C++ program to find number of consonants in given string
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
char str[20];
int consonant=0;
cout<<"Enter the string ";
gets(str);
for(int i=0;str[i]!='\0';i++)
{
if(str[i]>'z'||str[i]<'A'||(str[i]>'Z'&&str[i]<'a'))
continue;
if(str[i]<'a')
str[i]+=32;
if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u')
continue;
consonant++;
}
cout<<"Number of consonant "<<consonant;
return 0;
}
#include<stdio.h>
using namespace std;
int main()
{
char str[20];
int consonant=0;
cout<<"Enter the string ";
gets(str);
for(int i=0;str[i]!='\0';i++)
{
if(str[i]>'z'||str[i]<'A'||(str[i]>'Z'&&str[i]<'a'))
continue;
if(str[i]<'a')
str[i]+=32;
if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u')
continue;
consonant++;
}
cout<<"Number of consonant "<<consonant;
return 0;
}
No comments:
Post a Comment