3

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
11
  • Are you sure its not working? what example u tried? Commented Nov 9, 2015 at 0:04
  • well I used the word desserts which backwards is stressed and it says its not a palindrome... Commented Nov 9, 2015 at 0:07
  • the issue u r having is that u r only returning true for palindrome if the all the words are palindrome itself... Commented Nov 9, 2015 at 0:07
  • 1
    the word desserts is not a palindrome, right? Commented Nov 9, 2015 at 0:08
  • This is what the assignment says: 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. 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 Commented Nov 9, 2015 at 0:14

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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]))

8 Comments

I added what you said to try and delete special characters but I am getting 'remove_if' was not declared in this scope
@mb13 #include <algorithm>
Question, When I run it on cpp.sh it works great, but when I try running it on secure shell, I am getting these errors on the lambda, **error: expected primary-expression before â[â token **error: expected primary-expression before â]â token **error: expected primary-expression before âcharâ
Most likely it's an old compiler that is not compatible with C++11, so it does not understand lambda. @mb13
Do you happen to know another way to remove special characters?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.