1. Write a recursive function that accepts a string and returns the number of vo
ID: 3759568 • Letter: 1
Question
1. Write a recursive function that accepts a string and returns the number of vowels in provided string. Output should look like: Type a sentence My name is Alihan There are 6 vowels in the given sentence.
2. A palindrome is any word, phrase, or sentence that reads the same forward and backward. Here are some well-known palindromes:
Able was I, ere I saw Elba
A man, a plan, a canal, Panama
Desserts, I stressed
Kayak
Write a bool function that uses recursion to determine if a string argument is a palindrome. The function should return true if the argument reads the same forward and backward. Demonstrate the function in a program.
This what I have so far.
#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;
void displayMenu();
const int SIZE = 50;
//For Consonants
int remCons();
int consonant(int len);
char str[SIZE];
int count = 0;
//For Palindromes
int enterPal();
bool isPal(string, int, int);
string userPal[SIZE];
int main()
{
int choice;
const int cons = 1, //choice for removing consonants
pals = 2; //choice for finding palindromes
cout << "This program allows you to enter a word or phrase and either /n"
<< "remove the consonants or check if it's a palindrome or not/n";
displayMenu();
if (choice == 1)
remCons();
else if (choice == 2)
enterPal();
else
{
cout << "Invalid choice";
}
return 0;
}
void displayMenu()
{
cout << "Please choose which option you would like:";
cout << "1. Remove consonants";
cout << "2. Check for Palindrome";
}
int remCons()
{
int ans1, ans2, len;
cout << "Enter your sentence or word no more than 50 characters: ";
cin.getline(str, SIZE);
len = strlen(str);
ans1 = consonant(len);
cout << "Total consonants are: " << ans1 << endl;
}
int consonant(int len)
{
if (len == 0)
{
return count;
}
if (str[len - 1] != 'A' || str[len - 1] != 'a' || str[len - 1] != 'E' || str[len - 1] != 'e' ||
str[len - 1] != 'I' || str[len - 1] != 'i' || str[len - 1] != 'O' || str[len - 1] != 'o' ||
str[len - 1] != 'U' || str[len - 1] != 'u')
{
count++;
}
count = consonant(len - 1);
return count;
}
int enterPal()
{
cin.ignore();
cout << "Enter your sentence or word no more than 50 characters: ";
cin.getline(userPal, SIZE);
isPal(userPal, 0, userPal.length() - 1);
}
bool isPal(string str, int start, int end)
{
if (start >= end)
return true;
if (str[start] != str[end])
return false;
return isPal(str, ++start, --end);
}
Explanation / Answer
#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;
void displayMenu();
const int SIZE = 50;
//For Consonants
void vowels();
int numberOfVowels(string , int len);
char str[SIZE];
//For Palindromes
void enterPal();
bool isPal(string, int, int);
char userPal[SIZE];
int main()
{
int choice;
const int cons = 1, //choice for removing consonants
pals = 2; //choice for finding palindromes
cout << "This program allows you to enter a word or phrase and either count vowels or check if it's a palindrome or not ";
displayMenu();
cout<<" Enter: ";
cin>>choice;
if (choice == 1)
vowels();
else if (choice == 2)
enterPal();
else
{
cout << "Invalid choice";
}
return 0;
}
void displayMenu()
{
cout << " Please choose which option you would like:";
cout << " 1. Count Vowels";
cout << " 2. Check for Palindrome";
}
void vowels()
{
cin.ignore();
int ans1, len;
cout << "Enter your sentence or word no more than 50 characters: ";
string s;
getline(cin, s);
len = s.length();
ans1 = numberOfVowels(s, len);
cout << "Total vowels are: " << ans1 << endl;
}
int numberOfVowels(string str, int len)
{
if (len == 0)
{
return 0;
}
if (str[len - 1] == 'A' || str[len - 1] == 'a' || str[len - 1] == 'E' || str[len - 1] == 'e' ||
str[len - 1] == 'I' || str[len - 1] == 'i' || str[len - 1] == 'O' || str[len - 1] == 'o' ||
str[len - 1] == 'U' || str[len - 1] == 'u')
{
return 1 + numberOfVowels(str, len - 1);
}
return 0 + numberOfVowels(str, len - 1);
}
void enterPal()
{
cin.ignore();
cout << "Enter your sentence or word no more than 50 characters: ";
string s;
getline(cin, s);
int len = s.length();
cout<<s<<" is"<<(isPal(s, 0, len - 1) ? "" : " not")<<" a palindrome ";
}
bool isPal(string str, int start, int end)
{
if (start >= end)
return true;
if (str[start] != str[end])
return false;
return isPal(str, ++start, --end);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.