5 Replies - 2306 Views - Last Post: 10 October 2012 - 04:41 PM Rate Topic: -----

#1 k24bfan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-June 11

Palindrome ignore everything but char

Posted 10 October 2012 - 02:46 PM

I just want to know what is wrong with my implementation of my palindrome program. I got it working for simple cases such as "civic" but I also want it to ignore white space,punctuation, and numbers. I am trying to use isalpha(). Also I am trying to properly return the [right] index value if its not a palindrome. Its not working with cases such as "Red rum, sir, is murder!"
Any help is appreciated thanks
#include <string.h>
#include <ctype.h>

int palindrome(char * s){
	int right;
	right = (int)strlen(s)-1;
	int left =0;
	
	while(left<right){
		while(!isalpha(s[left])){left++;}
		while(!isalpha(s[right])){right++;}
		if(toupper(s[left])!=toupper(s[right])){return right;}
				
				
				left++;
				right--;}
	return 0;
	}
	
	int main(){
		printf("%d",palindrome("racecar")); return 0;
	}



Is This A Good Question/Topic? 0
  • +

Replies To: Palindrome ignore everything but char

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6111
  • View blog
  • Posts: 23,671
  • Joined: 23-August 08

Re: Palindrome ignore everything but char

Posted 10 October 2012 - 03:19 PM

I would consider copying the string without the offending characters into a new string, and use that to test for the palindrome.
Was This Post Helpful? 0
  • +
  • -

#3 k24bfan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-June 11

Re: Palindrome ignore everything but char

Posted 10 October 2012 - 03:30 PM

Yes that is what I original approach but I was told that was not needed.
So I should just build a function that filters through the string and then check if its palindrome.
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5940
  • View blog
  • Posts: 12,866
  • Joined: 16-October 07

Re: Palindrome ignore everything but char

Posted 10 October 2012 - 03:33 PM

At first glance it looked like it would work. Aside from the fact the boolean is reversed, your s[right] is completely going the wrong direction. And indent style might help.

int palindrome(const char *s){
	int right = strlen(s)-1;
	int left = 0;
	
	while(left<right){
		while(!isalpha(s[left])){left++;}
		while(!isalpha(s[right])){right++;} // think about this
		if(toupper(s[left])!=toupper(s[right])){return right;} // true?  Are you sure?  Don't get too cute.
		left++;
		right--;
	}
	return 0; // if you get here, 1 would be a reasonable result
}



You still run the risk of (left<right) being true after one of those whiles.
Was This Post Helpful? 0
  • +
  • -

#5 k24bfan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-June 11

Re: Palindrome ignore everything but char

Posted 10 October 2012 - 03:57 PM

View Postbaavgai, on 10 October 2012 - 03:33 PM, said:

At first glance it looked like it would work. Aside from the fact the boolean is reversed, your s[right] is completely going the wrong direction. And indent style might help.

int palindrome(const char *s){
	int right = strlen(s)-1;
	int left = 0;
	
	while(left<right){
		while(!isalpha(s[left])){left++;}
		while(!isalpha(s[right])){right++;} // think about this
		if(toupper(s[left])!=toupper(s[right])){return right;} // true?  Are you sure?  Don't get too cute.
		left++;
		right--;
	}
	return 0; // if you get here, 1 would be a reasonable result
}



You still run the risk of (left<right) being true after one of those whiles.


changed right++ to right--;
if(toupper(s[left])!=toupper(s[right])){return right;}
What is wrong with it? Purpose is to return the index from the right side is not a palindrome(ignoring spaces, and punctuation). I tested it with some larger strings it seems to work. Thanks for the help.
Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5940
  • View blog
  • Posts: 12,866
  • Joined: 16-October 07

Re: Palindrome ignore everything but char

Posted 10 October 2012 - 04:41 PM

View Postk24bfan, on 10 October 2012 - 06:57 PM, said:

What is wrong with it? Purpose is to return the index from the right side is not a palindrome(ignoring spaces, and punctuation).


It does depend on you expectations of the result value. The position at which you fail, at the tail, is one type of result.

However, if you expect a boolean result, 0 is false, !0 is true, and your result would seem counter intuitive.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1