edward_elric
edward_elric

Reputation: 11

trying to implement a recursive version of detecting a palindrome within a string using C++. having some trouble here

Having trouble trying to implement a recursive version for detecting a palindrome. Cannot get correct output :(

#include <iostream>
#include <string>
using namespace std;

bool testPalindrome( string, unsigned int length, int begin );

int main()
{
    string test;
    cout << "Enter what you wish to test for a palindrome: ";
    cin >> test;
    unsigned int len = test.length(); // acquire length of string
    int beginning = 0; // set variable to point to beginning of string

    if ( testPalindrome( test, len, beginning ) )
       cout << "PALINDROME!" << endl;
    else
        cout << "NOTHING" << endl;
}

The code above is the main function I am using to test the palindrome function I am implementing. Below, I have also provided the code I wrote to detect a palindrome.

// implemented recursive function to check for a palindrome
bool testPalindrome( string name, unsigned int len, int begin )
{ 
   // conditional to determine if beginning char position is equal to last char

  if ( begin >= len )
    return true; // if so, return true
  // check if characters are equal, if not return false
  else if ( name[ begin ] != name[ len ] )
      return false;
  // general case, call function with positions of characters being tested
  // shifted by one slot each, respectively
  else
     return testPalindrome( name, ( len - 1 ), ( begin + 1 ) );
}

Upvotes: 0

Views: 97

Answers (3)

Chintan Ghate
Chintan Ghate

Reputation: 189

#include <iostream>
#include <string>

using namespace std;

bool isPalindrome(string S, int len, int index) {
    while (index <= len / 2) {
        return (S[index] == S[len - index - 1]) && isPalindrome(S, len, index + 1); 
    }
    return true;
}

int main() {
    string S = "racecar";
    if (isPalindrome(S, S.size(), 0)) {
        cout << "Is a Palindrome\n";
    } else {
        cout << "Not a Palindrome\n";
    }
    return 0;
}

This should do it!

In the code that you wrote, while calling the function, use :

testPalindrome(name, len - 1, 0);

instead of:

testPalindrome(name, len, 0);

And since you are passing len - 1 as the argument, change type of len from unsigned int to int to prevent errors with NULL strings.

And add a condition checker :

if (len == -1) {
    return true;
}

NULL strings are palindromes. without this condition your code will through SEGMENTATION FAULT for NULL strings.

Upvotes: 1

Rudolf Polzer
Rudolf Polzer

Reputation: 345

Change the center line in testPalindrome() to

else if ( name[ begin ] != name[ len - 1 ] )

because name[len] reads a character behind the end of your string.

Upvotes: 0

Hawkmooon
Hawkmooon

Reputation: 508

You haven't done a good job explaining what exactly your problem is, but I suspect your issue is that you're indexing len into name instead of len - 1. Indexing into strings is zero-indexed and not one-indexed so the index len is invalid.

Upvotes: 2

Related Questions