4 Replies - 854 Views - Last Post: 23 March 2013 - 11:41 AM Rate Topic: -----

#1 CompProgNoob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 21-March 13

Check if a word is a palindrome and avoid punctuation and spaces.

Posted 21 March 2013 - 10:03 AM

I have to make a program with a bool function to check if a word or phrase is or isn't a palindrome, and the function has to be recursive. I have been looking online a lot, and of course I could just copy one, but I really want to understand it. As of right now I do not have anything that will check if the string contains any punctuation or white spaces. I don't know exactly where to start with that task. However, when I type in 'kayak' is keeps telling me it is not a palindrome.

Any help given would be much appreciated. I normally like to figure things out for myself, but this one has me stumped.

Thanks,

PS I only know basic coding commands, we haven't done extensive work on inbuilt functions and all, so my code will probably look a little, well basic, and that is because that is what I know. :)/>


#include <iostream>
#include <String>
using namespace std;

//Function prototype
bool palindrome(string , int , int  );


int main()
{
    string word;
    int b , e;
    
    
    //call the function.
    if (palindrome(word,b,e) == true)
       cout<< word << " Is a palindrome." << endl << endl;
    else if (palindrome(word,b,e) ==false)
       cout << word << " Is not a palindrome." << endl << endl;
   
    system("PAUSE");
    return 0;
    
    
}

bool palindrome(string test, int b , int e )
{
    b = 0;
    e = test.size();
    
    if(b >=e)
        return true;
    if(test[b] != test[e])
        return false;
    if((test[b])== (test[e]))
    {
        
        return palindrome(test,b+1,e-1);
    }   
   return true;   
}





Is This A Good Question/Topic? 0
  • +

Replies To: Check if a word is a palindrome and avoid punctuation and spaces.

#2 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 765
  • View blog
  • Posts: 2,225
  • Joined: 20-March 10

Re: Check if a word is a palindrome and avoid punctuation and spaces.

Posted 21 March 2013 - 12:27 PM

Hi,

You cant have

b = 0;
e = test.size();

in your function


it has to go before you call the function in main.
so

b = 0;
e = test.size();
//call the function.
if (palindrome(word,b,e) == true)
cout<< word << " Is a palindrome." << endl << endl;
else if (palindrome(word,b,e) ==false)
cout << word << " Is not a palindrome." << endl << endl;


Even then your function will only detect odd numbered words.


Regards


Snoopy.
Was This Post Helpful? 0
  • +
  • -

#3 CompProgNoob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 21-March 13

Re: Check if a word is a palindrome and avoid punctuation and spaces.

Posted 22 March 2013 - 06:38 AM

View Postsnoopy11, on 21 March 2013 - 12:27 PM, said:

Hi,

You cant have

b = 0;
e = test.size();

in your function


it has to go before you call the function in main.
so

b = 0;
e = test.size();
//call the function.
if (palindrome(word,b,e) == true)
cout<< word << " Is a palindrome." << endl << endl;
else if (palindrome(word,b,e) ==false)
cout << word << " Is not a palindrome." << endl << endl;


Even then your function will only detect odd numbered words.


Regards


Snoopy.


Thanks, I changed it and now it doesn't even give me the oppurtunity to enter in a word. Any suggestions why it would do that?

View Postsnoopy11, on 21 March 2013 - 12:27 PM, said:

Hi,

You cant have

b = 0;
e = test.size();

in your function


it has to go before you call the function in main.
so

b = 0;
e = test.size();
//call the function.
if (palindrome(word,b,e) == true)
cout<< word << " Is a palindrome." << endl << endl;
else if (palindrome(word,b,e) ==false)
cout << word << " Is not a palindrome." << endl << endl;


Even then your function will only detect odd numbered words.


Regards


Snoopy.


Thanks, I changed it and now it doesn't even give me the oppurtunity to enter in a word. Any suggestions why it would do that?

View Postsnoopy11, on 21 March 2013 - 12:27 PM, said:

Hi,

You cant have

b = 0;
e = test.size();

in your function


it has to go before you call the function in main.
so

b = 0;
e = test.size();
//call the function.
if (palindrome(word,b,e) == true)
cout<< word << " Is a palindrome." << endl << endl;
else if (palindrome(word,b,e) ==false)
cout << word << " Is not a palindrome." << endl << endl;


Even then your function will only detect odd numbered words.


Regards


Snoopy.


Thanks, I changed it and now it doesn't even give me the opportunity to enter in a word. Any suggestions why it would do that?
Was This Post Helpful? 0
  • +
  • -

#4 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 765
  • View blog
  • Posts: 2,225
  • Joined: 20-March 10

Re: Check if a word is a palindrome and avoid punctuation and spaces.

Posted 22 March 2013 - 07:28 AM

I meant something like this
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;

bool IsPalindrome(string word,int begins,int ends)
{
if (word=="") return false;

if (begins==ends) return true;
else
if (begins<ends)
{
if (word[begins]==word[ends]) return IsPalindrome(word,begins+1,ends-1);
else return false;
}
return false;
}

int main()
{

	string  palindrome;
	palindrome.append("kayak");
	if(IsPalindrome(palindrome,0,(palindrome.size()-1)))
		cout <<"Palindrome detected";
	else
        cout <<"Palindrome not detected";

	_getch();// Used to hold window open replace as you see fit//
	return 0;
}


But as I say this will fail on even numbered Palindromes

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#5 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 277
  • View blog
  • Posts: 1,783
  • Joined: 20-September 08

Re: Check if a word is a palindrome and avoid punctuation and spaces.

Posted 23 March 2013 - 11:41 AM

I didn't see any failure on testing even number strings :)

bool test( const string& s, int b, int e )
{
   while( b < e && ! isalnum( s[ b ] ) ) ++ b;
   while( e > b && ! isalnum( s[ e ] ) ) -- e;

   if( b >= e ) return true;

   if( tolower( s[ b ] ) == tolower( s[ e ] ) ) return test( s, b+1, e-1 );
   else return false;
}


This post has been edited by David W: 23 March 2013 - 11:51 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1