Reputation: 51
I am creating a program that uses recursion functions to count the vowels in a sentence and to determine if it is a palindrome. The problem I am getting is that it says the sentence entered is not palindrome even if it is.. Any help with this will be greatly appreciated. Thank you.
#include<iostream>
#include <cmath>
using namespace std;
struct Sentence
{
int CountVowels(string , int);
public:
Sentence (string);
bool isPal(string , int);
void Print();
string s;
int numVowel;
int length;
//~Sentence();
};
Sentence :: Sentence (string b)
{
s = b;
length = 0;
numVowel = CountVowels(s, 0);
}
int Sentence :: CountVowels(string myWord, int startindex)
{
length ++;
int pandi;
if(myWord[startindex])
{
if (myWord[startindex] != 'a' && myWord[startindex] != 'e' && myWord[startindex] != 'i' && myWord[startindex] != 'o' && myWord[startindex] != 'u')
{
pandi = 0;
}
else pandi = 1;
return pandi + CountVowels(myWord, startindex + 1);
}
return 0;
}
bool Sentence :: isPal(string myWord, int size)
{
int r = myWord.size() - size;
int t = size - 1;
if (size == r || r == t)
return true;
if ((myWord[r]) != (myWord[t]))
return false;
return isPal(myWord, -- size);
}
void Sentence :: Print()
{
cout << s [-- length];
if (length == 0)
{
cout << endl;
return;
}
Print ();
}
/*Sentence :: ~Sentence()
{
cout << "\ntilde delete\n\n";
}*/
int main ()
{
string userW;
cout << "Enter a sentence: \n";
getline(cin, userW);
userW.erase(remove_if(userW.begin(), userW.end(), [](char c) {return !isalpha(c); }), userW.end());
Sentence userSent(userW);
cout << "The number of vowels in the sentence is " << userSent.numVowel << endl;
cout << "" << endl;
cout << "The sentence " << userSent.s << " is" <<
(userSent.isPal(userSent.s, userSent.s.size()) ? " Palindrome\n" : " Not Palindrome\n");
return 0;
}
UPDATE: I am now trying to remove special characters. So it looks like this
string userW;
cout << "Enter a sentence: \n";
getline(cin, userW);
userW.erase(remove_if(userW.begin(), userW.end(), [](char c) {return !isalpha(c); }), userW.end());
But I am getting this error:
In function 'int main()':
88:85: error: 'remove_if' was not declared in this scope
Upvotes: 3
Views: 342
Reputation: 29332
Your code is fine except that it does not remove commas, spaces and anything that is not alphabetic from the sentence. Also, you need to do a case-insensitive comparison on the characters. This is required, otherwise the examples
A man, a plan, a canal, Panama
Desserts, I stressed
would not be palidromes.
To remove special characters from the user input, you can use lambda
string userW; cout << "Enter a sentence: \n"; getline(cin, userW);
userW.erase(remove_if(userW.begin(), userW.end(), [](char c) {return !isalpha(c); }), userW.end());
EDIT you can also try the following to avoid the need for lambda:
userW.erase(std::copy_if(userW.begin(), userW.end(), userW.begin(), isalpha), userW.end());
to do a case-insensitive comparison, in the function isPal, you can change this:
if ((myWord[r]) != (myWord[t]))
into this:
if (tolower(myWord[r]) != tolower(myWord[t]))
Upvotes: 1
Reputation: 338
I have reviewed your program. You are trying to out the string in the function pirnt(). Here is the problem ,when you use
cout << "The sentence backwards is: " << userSent.Print();
but funtion Print() does not have any return type.(because this is void type). Here you should use
cout << "The sentence backwards is: " ;
userSent.Print();
and now it works.
Upvotes: 1