|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
//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]
__________________
-- Manuel Hirsch - Linux, FreeBSD, programming, administration articles, tutorials and more. |
|
#3
|
||||
|
||||
|
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);
}
}
|
|
#4
|
|||
|
|||
|
what do we get for making you pass this class? (only kidding
) |
|
#5
|
|||
|
|||
|
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 |
|
#6
|
|||
|
|||
|
honestly, was it homework for school?
![]() |
|
#7
|
|||
|
|||
|
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
![]() |
|
#8
|
|||
|
|||
|
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;
}
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > palindrome recursion |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|