#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2002
    Posts
    20
    Rep Power
    0

    palindrome recursion


    Hi

    I'm a beginner, currently I'm working on a program that determines whether a word is a palindrome, in which a word can be spelled backwards and still remain the same ie. MOM, DAD, spelled backwards is still MOM,DAD etc.
    However I keep on getting error "Invalid direction" Below is my code. And I also made an attachment of the code as well. I'm trying to figure it out using recursion. Can someone please tell me what is the problem.

    #include <iostream.h>
    #include <conio.h>
    #include <string.h>
    using namespace std;

    //---------------------------------------------------------------------------

    bool Palindrome(int first, int last, char inputString[]);

    int main()
    {
    int first = 0;// first index for array
    int last = 0;// last index for array
    char inputString[100];//initialize size of array


    cout << "This program determines whether the word"
    << endl
    << "or sentence entered by user is a palindrome."
    << endl;

    cout << "\n\nPlease enter a word or phrase: ";

    cin >> inputString;//receive input from user
    last = strlen(inputString);//store input into character array

    if (Palindrome(first,last,inputString)== true)
    cout << "Input is classified as a palindrome.";
    else
    cout << "Input is not classifed as a palindrome.";

    getch();
    return 0;
    }

    bool Palindrome(int first, int last, char inputString)
    {

    //if next first element and next last element are not the same
    //therefore word is not a palindrome returned as false
    if (inputString[first]!= inputString[last])
    return false;

    //if next first element and next last element are same
    //call Palindrome function again, increment first element by one
    //decrement last element by one to compare if those two elements
    //in array are equal.Continue this process until next first element
    //and next element are not equal.
    else if (inputString[first] == inputString[last])
    {
    Palindrome((first+1),(last+1),inputString);
    return true;
    }
    //if the next first index is greater than the next last index
    //program concludes that all the elements in array have been compared
    //and therefore input should be a palindrome retuned as true.
    else if (inputString[first] > inputString[last])
    {
    return true ;
    }
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    190
    //decrement last element by one to compare if those two elements
    //in array are equal.Continue this process until next first element
    //and next element are not equal.
    else if (inputString[first] == inputString[last])
    {
    Palindrome((first+1),(last-1),inputString);
    return true;
    }

    it didnīt read the full code, but if it still does not work, i will ;)

    [edit]
    sorry, you probably cannot see that the "-" is highlighted (for me in IE6, i cannot). you have "+" there...
    and i think i found another error:
    else if (inputString[first] > inputString[last])
    imho should be
    else if (first > last)
    and you need to redo your if-construction because of this...
    if you would use the [ code ] tags and ident your code, it would be much easier to see that the line as you typed it is never evaluated since "inputString[first] != inputString[last]" precedes "inputString[first] > inputString[last]"
    (there is no three boolean states ;) )
    [/edit]
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    There are quite a few errors in your code:

    Code:
    last = strlen(inputString);//store input into character array 
    
    if (Palindrome(first,last,inputString)== true)
    should be Palindrome(first, last-1, ....) because array is zero based.

    Also in this section:
    Code:
    else if (inputString[first] == inputString[last]) 
    { 
    Palindrome((first+1),(last+1),inputString); 
    return true; 
    }
    Two errors here. First it should be (last - 1) instead of (last + 1). Also you're completely ignoring the return value of Palindrome when you're recursing into the function and arbitarily returning true. So if your input string is "foef", the function will still return true. You should modify it to:
    Code:
    else if (inputString[first] == inputString[last]) 
    { 
      return Palindrome((first+1),(last-1),inputString); 
    }
    Finally, this statement:
    else if (inputString[first] > inputString[last])

    This should be if (first > last) and also this should be the first thing checked in the function before you check anything else. Otherwise, there is a chance that one of the other if checks will be satisfied earlier and this code will never be executed.

    Here's the corrected code. Note that it is still not technically correct, because it is not case-insensitive, but that is left as an exercise for you to complete:
    Code:
    #include <iostream.h>
    #include <conio.h>
    #include <string.h>
    using namespace std;
    
    //---------------------------------------------------------------------------
    
    bool Palindrome(int first, int last, char inputString[]);
    
    int main()
    {
      int first = 0;// first index for array
      int last = 0;// last index for array
      char inputString[100];//initialize size of array
    
    
      cout << "This program determines whether the word"
           << endl
           << "or sentence entered by user is a palindrome."
           << endl;
    
      cout << "\n\nPlease enter a word or phrase: ";
    
      cin >> inputString;//receive input from user
      last = strlen(inputString);//store input into character array
    
      if (Palindrome(first,last - 1,inputString)== true)
        cout << "Input is classified as a palindrome.";
    else
      cout << "Input is not classifed as a palindrome.";
    
      getch();
    
      return 0;
    }
    
    bool Palindrome(int first, int last, char *inputString)
    {
    
      //if the next first index is greater than the next last index
      //program concludes that all the elements in array have been compared
      //and therefore input should be a palindrome retuned as true.
      if (first > last)
        return true;
    
      //if next first element and next last element are not the same
      //therefore word is not a palindrome returned as false
      if (inputString[first]!= inputString[last])
        return false;
    
      //if next first element and next last element are same
      //call Palindrome function again, increment first element by one
      //decrement last element by one to compare if those two elements
      //in array are equal.Continue this process until next first element
      //and next element are not equal.
      else if (inputString[first] == inputString[last])
        {
          return Palindrome((first+1),(last-1),inputString);
        }
    }
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    190
    what do we get for making you pass this class? (only kidding ;) )
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2002
    Posts
    20
    Rep Power
    0

    Smile thanx


    Thanx for thorough evaluation of my code, greatly appreciated. Been trying to figure out the entire morning and finally figured out the errors.

    Thanx again ,

    getchoo
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    190
    honestly, was it homework for school? ;)
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Posts
    0
    Rep Power
    0
    Heh, I had the same assignment in my Comp Sci class a few years back, but I went about it differently. I had two arrays, string[], and string_reverse[], string_reverse was populated through a for loop that put string[x] into string_reverse[strlen(string)-x], then another for loop to compare the two strings to each other, if they're the same, then it returns true. I do like the way you did it though :D
  14. #8
  15. No Profile Picture
    Code Poet
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Location
    Boston
    Posts
    9
    Rep Power
    0
    Slightly shorter:

    Code:
    #include <string>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    // checks to see if a word is a palindrome
    // does not check case.
    int main() {
      string forw,back;
      cout << "Please enter string: ";
      cin >> forw;
      back = forw;
      reverse(back.begin(), back.end());
      if (forw == back) cout << "Simple word palindrome." << endl;
      else cout << "Not a simple word palindrome." << endl;
      return 0;
    }

IMN logo majestic logo threadwatch logo seochat tools logo