11 Replies - 9375 Views - Last Post: 27 October 2010 - 06:11 PM Rate Topic: -----

#1 GuyArcher  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 28-March 08

Palindrome recursive function driving me nuts

Posted 07 May 2009 - 07:10 AM

Alright. So, I've been struggling with creating a program that checks for the almighty palindrome. I managed to create some loops to clean up the input (remove everything but letters and make them all lowercase) but I'm struggling with the bool recursive function I need to write for the actual Palindrome test. I don't get any compilation errors (so I know it's likely a logic problem) so hoping someone can point out what I'm doing wrong. Keep in mind that I'm pretty much a beginner so my code is likely sloppy and not the most stream-lined. At this point, I'm just trying to get it to test using the arrays I've set up. I could swap to strings but that's not a topic we've covered and it would force me to rewrite the clean up loops as I'm under the impression that strings don't work with isupper, islower, toupper, etc.

Please help before I go bald ripping my hair out! Here's my function.

bool isPalin(char* str)
{
	int len = strlen(str);
	char str4[86] = {0};//initialize string
	int j, k;


	if (str[0] != str[len-1])  //If statement to test first and last character of string
	{
		return 0;
	}

	if (str[len] == 1) //If statement to allow for odd number of characters when it reaches the middle.
	{
		return 1;
	}


	for(j = 0; str[j]; j++)
	{
		if(j >= 1)
		{
			if(j == len)
			{
				break;
			}
			str4[j] = str[j];
		}

	}
	return isPalin(str4);
}



Is This A Good Question/Topic? 1
  • +

Replies To: Palindrome recursive function driving me nuts

#2 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: Palindrome recursive function driving me nuts

Posted 07 May 2009 - 07:47 AM

Oh, but it does work with strings! Check out this other program, similar to yours, I think:
PalindromeChecker

I may be wrong, but I think you would have to use strings, since a char is one character. You can't really check that for being a palindrome, can you?
Was This Post Helpful? 0
  • +
  • -

#3 Jubb  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 88
  • Joined: 06-May 09

Re: Palindrome recursive function driving me nuts

Posted 07 May 2009 - 07:57 AM

He's using an array of characters. You can do it either way, but some things I would suggest are checking the length before checking if it's a palindrome (more than 1 or 2 characters), and using iterators.
Was This Post Helpful? 0
  • +
  • -

#4 GuyArcher  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 28-March 08

Re: Palindrome recursive function driving me nuts

Posted 07 May 2009 - 08:00 AM

View PostOliveOyl3471, on 7 May, 2009 - 06:47 AM, said:

Oh, but it does work with strings! Check out this other program, similar to yours, I think:
PalindromeChecker

I may be wrong, but I think you would have to use strings, since a char is one character. You can't really check that for being a palindrome, can you?


I'll have another look at it. I saw his/her code earlier but they don't use a recursive function which is a requirement for this assignment. I may have better luck with strings though so I'll give it a shot. :)

View PostJubb, on 7 May, 2009 - 06:57 AM, said:

He's using an array of characters. You can do it either way, but some things I would suggest are checking the length before checking if it's a palindrome (more than 1 or 2 characters), and using iterators.


Good idea on the length check. Iterators aren't something we've covered so I'm a bit hesitant (as I am with strings) to attempt to use them. Guess it couldn't hurt to read more about it.

My biggest issue with this class is that we jumped from Chapter 9 in our textbook straight to chapter 19 so there's A LOT in the middle that we haven't covered at all. I'm guessing that's being saved for the next level of class.
Was This Post Helpful? 0
  • +
  • -

#5 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: Palindrome recursive function driving me nuts

Posted 07 May 2009 - 08:01 AM

OH! *slaps forehead* An array of characters. Of course.

Sorry if I confused you. heh. :blush:
Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5941
  • View blog
  • Posts: 12,869
  • Joined: 16-October 07

Re: Palindrome recursive function driving me nuts

Posted 07 May 2009 - 08:03 AM

Oops!
return isPalin(str4);



Did you mean to use recursion? You have placed yourself in an infinite loop.

I'm afraid your code isn't actually doing anything constructive.

bool isPalin(char* str) {
	int len = strlen(str);
	char str4[86] = {0};//initialize string
	int j, k;


	// this should be how your loop works
	// if (str[0] != str[len-1]) { return 0; }

	// doesn't matter if your loop works
	// if (str[len] == 1) { return 1; }


	for(j = 1; str[j]!=0; j++) {
		// no need to check twice
		// if(j == len) { break; }
		// so you're just copying
		str4[j] = str[j];
	}
	// send a copy to this function.
	// do this forever
	return isPalin(str4);
}



This was the best piece of code you had: "if (str[0] != str[len-1]) { return 0; }"

Now, check str[1] and str[len-2]. Keep doing that until it makes sense to stop.

Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#7 GuyArcher  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 28-March 08

Re: Palindrome recursive function driving me nuts

Posted 07 May 2009 - 08:10 AM

View Postbaavgai, on 7 May, 2009 - 07:03 AM, said:

Oops!
return isPalin(str4);



Did you mean to use recursion? You have placed yourself in an infinite loop.

I'm afraid your code isn't actually doing anything constructive.

bool isPalin(char* str) {
	int len = strlen(str);
	char str4[86] = {0};//initialize string
	int j, k;


	// this should be how your loop works
	// if (str[0] != str[len-1]) { return 0; }

	// doesn't matter if your loop works
	// if (str[len] == 1) { return 1; }


	for(j = 1; str[j]!=0; j++) {
		// no need to check twice
		// if(j == len) { break; }
		// so you're just copying
		str4[j] = str[j];
	}
	// send a copy to this function.
	// do this forever
	return isPalin(str4);
}



This was the best piece of code you had: "if (str[0] != str[len-1]) { return 0; }"

Now, check str[1] and str[len-2]. Keep doing that until it makes sense to stop.

Hope this helps.



Thanks for the comments. I'll drill deeper and see what I can figure out but I have a feeling that you've gotten me where I need to be. I do have to use recursion though.
Was This Post Helpful? 0
  • +
  • -

#8 GuyArcher  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 28-March 08

Re: Palindrome recursive function driving me nuts

Posted 07 May 2009 - 08:59 AM

Thanks so much for all the help! Works like a charm now. Fixing the infinite loop definitely helped.
Was This Post Helpful? 0
  • +
  • -

#9 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: Palindrome recursive function driving me nuts

Posted 07 May 2009 - 12:51 PM

When it comes to code, you should always listen to baavgai. There's a reason his badge says 'Expert' :D
Was This Post Helpful? 0
  • +
  • -

#10 sk8doc6  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 27-October 10

Re: Palindrome recursive function driving me nuts

Posted 27 October 2010 - 01:40 PM

//crated by C. House
//the simplest and most effiecent palindrome checker
//works 100% with strings that have spaces and punctuation marks
//10-27-10

#include <iostream>
#include <string>

using namespace std;

int main()
{
	string pal, ans;
	bool test(string pal);
	
	
			cout<<"Please enter a palindrome:"<<endl;
			getline( cin, pal );

		
			test(pal);

			if (test(pal) == true)
				cout<<pal<<" is a palindrome"<<endl;
			else
				cout<<pal<<" is not a palindrome"<<endl<<endl;

		
	system("pause");
		
	return 0;
}

bool test(string pal)
{  if (pal.length() <= 1) 
		return true;
   return 
	   (pal[0] == pal[pal.length() - 1] && test(pal.substr(1, pal.length() - 2)));
}

// substr provides the ability to use negative 'start' position and negative lengths.
//got the idea to use substr function from http://www.zedwood.c...substr-function

This post has been edited by sk8doc6: 27 October 2010 - 01:46 PM

Was This Post Helpful? 0
  • +
  • -

#11 sk8doc6  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 27-October 10

Re: Palindrome recursive function driving me nuts

Posted 27 October 2010 - 06:05 PM

This is the same program I wrote before, only this time if puncutation is put in, it will skip over puncutaion and keep testing
/*************************************************************
*
* Names: Chris House (crh225)          Date Assigned: October 27
*        Liberty McMillan (lm806)

* Course: CSE 1384                     
*
* File name: criver.cpp
*
* File Description:
* This program checks to see if the input is a palindrome
* 
**************************************************************/

#include <iostream>
#include <string>

using namespace std;

int main()
{
	string pal;
	bool test(string pal);
	
			//gets line of input untill enter key is pressed
			cout<<"Please enter a palindrome:"<<endl;
			getline( cin, pal );
					
			test(pal);

			if (test(pal) == true)
				cout<<pal<<" is a palindrome."<<endl;
			else
				cout<<pal<<" is not a palindrome."<<endl<<endl;
					
	system("pause");
		
	return 0;
}

bool test(string pal)

{ 
	string newstring,x;
	 
	//for loop that removes puncutation and stores in newstring
   for (unsigned int i=0; i<pal.length();i++)  
   {  
     if (isalnum(pal[i])) 
		 newstring += pal[i]; 
   }  
 
   //begin recursive part
	if (newstring.length() <= 1) 
		return true;
   return 
	   (newstring[0] == newstring[newstring.length() - 1] && test(newstring.substr(1, newstring.length() - 2)));
}
// substr  provides the ability to use negative 'start' position and negative lengths. 
//got the idea to use substr function from http://www.zedwood.com/article/108/cpp-substr-function


Was This Post Helpful? 0
  • +
  • -

#12 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Palindrome recursive function driving me nuts

Posted 27 October 2010 - 06:11 PM

Your posting aren't really going to be helpful to the OP because this thread was completed in 2009.

You might want to explore this link
http://www.dreaminco...48-c-tutorials/
because that's probably more what you are trying to do with your posts.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1