The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
palindrome recursion
Discuss palindrome recursion in the C Programming forum on Dev Shed. palindrome recursion C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

May 22nd, 2002, 10:46 AM
|
|
Registered User
|
|
Join Date: Feb 2002
Posts: 20
Time spent in forums: < 1 sec
Reputation 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 ;
}
}
|

May 22nd, 2002, 01:39 PM
|
|
Contributing User
|
|
Join Date: Oct 2000
Location: Back in the real world.
|
|
// 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]
|

May 22nd, 2002, 02:01 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
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);
}
}
|

May 22nd, 2002, 02:06 PM
|
|
Contributing User
|
|
Join Date: Oct 2000
Location: Back in the real world.
|
|
what do we get for making you pass this class? (only kidding  )
|

May 22nd, 2002, 05:03 PM
|
|
Registered User
|
|
Join Date: Feb 2002
Posts: 20
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
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
|

May 23rd, 2002, 01:39 PM
|
|
Contributing User
|
|
Join Date: Oct 2000
Location: Back in the real world.
|
|
honestly, was it homework for school? 
|

June 4th, 2002, 01:47 PM
|
|
Junior Member
|
|
Join Date: Apr 2002
Posts: 0
Time spent in forums: < 1 sec
Reputation 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 
|

June 6th, 2002, 09:15 PM
|
|
Code Poet
|
|
Join Date: Jun 2002
Location: Boston
Posts: 9
Time spent in forums: < 1 sec
Reputation 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;
}
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|