Write a Menu Driven C++ program that creates a character array/string by taking input from user and perform following tasks by displaying menu to user, the menu operations are implemented using functions: a) Length of string.
b) Character, consonants and vowels count in a string
c) Palindrome check
d) Character search within the string. If found display its starting position.
e) covert to either case (i.e., if string is in lower case then convert it to upper case and vice versa.)
#include<iostream>
using namespace std;
int stringLength()
{ string myString;
cout << "String Length = " << myString.length();
}
void characterCount()
{
char line[150];
int vowels, consonants, digits, spaces;
vowels = consonants = digits = spaces = 0;
cout << "Enter a line of string: ";
cin.getline(line, 150);
for(int i = 0; line[i]!='\0'; ++i)
{
if(line[i]=='a' || line[i]=='e' || line[i]=='i' ||
line[i]=='o' || line[i]=='u' || line[i]=='A' ||
line[i]=='E' || line[i]=='I' || line[i]=='O' ||
line[i]=='U')
{
++vowels;
}
else if((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&& line[i]<='Z'))
{
++consonants;
}
else if(line[i]>='0' && line[i]<='9')
{
++digits;
}
else if (line[i]==' ')
{
++spaces;
}
}
cout << "Vowels: " << vowels << endl;
cout << "Consonants: " << consonants << endl;
cout << "Digits: " << digits << endl;
cout << "White spaces: " << spaces << endl;
}
void palindromeCheck()
{
int n,r,sum=0,temp;
cout<<"Enter the Number=";
cin>>n;
temp=n;
while(n>0)
{
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
cout<<"Number is Palindrome.";
else
cout<<"Number is not Palindrome.";
}
void characterSearch()
{
std::string myString ;
auto it = mystring.find("cat");
if (it != std::string::npos)
std::cout << "Found at position: " << it << '\n';
else
std::cout << "Not found!\n";
}
void caseConvert()
{
string myString;
// using transform() function and ::toupper in STL
transform(su.begin(), su.end(), su.begin(), ::toupper);
cout << su << endl;
// sl is the string which is converted to lowercase
string sl = "Jatin Goyal";
// using transform() function and ::tolower in STL
transform(sl.begin(), sl.end(), sl.begin(), ::tolower);
cout << sl << endl;
}
int main()
{
string myString;
cout<<"\n Enter any string you think of: ";
getline(cin, myString);
char option;
cout<<"\nSelect what you want to do";
cout<<"\n a. Length of string\n b. Character, consonants and vowels count in a string. \n c. Palindrome check.\n d.Character search within the string. If found display its starting position.\n e. covert to either case ";
cin>>option;
switch(option)
{
case 'a':
stringLength();
cout << "String Length = " << myString.length();
break;
case 'b':
characterCount();
break;
case 'c':
palindromeCheck();
break;
case 'd':
characterSearch();
break;
case 'e':
caseConvert();
break;
default:
cout<<"Invalid choice. Try again by entering a to e";
}
}
Comments
Leave a comment