C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old May 22nd, 2002, 10:46 AM
getchoo getchoo is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2002
Posts: 20 getchoo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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 ;
}
}

Reply With Quote
  #2  
Old May 22nd, 2002, 01:39 PM
M.Hirsch M.Hirsch is offline
Contributing User
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Oct 2000
Location: Back in the real world.
Posts: 5,969 M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 39 m 55 sec
Reputation Power: 184
//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.

Reply With Quote
  #3  
Old May 22nd, 2002, 02:01 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,442 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 1 h 49 m 40 sec
Reputation Power: 797
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);
    }
}

Reply With Quote
  #4  
Old May 22nd, 2002, 02:06 PM
M.Hirsch M.Hirsch is offline
Contributing User
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Oct 2000
Location: Back in the real world.
Posts: 5,969 M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 39 m 55 sec
Reputation Power: 184
what do we get for making you pass this class? (only kidding )

Reply With Quote
  #5  
Old May 22nd, 2002, 05:03 PM
getchoo getchoo is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2002
Posts: 20 getchoo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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

Reply With Quote
  #6  
Old May 23rd, 2002, 01:39 PM
M.Hirsch M.Hirsch is offline
Contributing User
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Oct 2000
Location: Back in the real world.
Posts: 5,969 M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 39 m 55 sec
Reputation Power: 184
honestly, was it homework for school?

Reply With Quote
  #7  
Old June 4th, 2002, 01:47 PM
avidal82 avidal82 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Posts: 0 avidal82 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #8  
Old June 6th, 2002, 09:15 PM
parker parker is offline
Code Poet
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Location: Boston
Posts: 9 parker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to parker
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;
}

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > palindrome recursion


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway