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