• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1025
  • Last Modified:

c program code for paliandrome using recursive

i want a c program code for checking whether the string is paliandrome or not using recursive method.i m a begginer in language environment and dont hav any idea of how to start
0
dinakarsoupaty
Asked:
dinakarsoupaty
3 Solutions
 
dinakarsoupatyAuthor Commented:
i want a answer quickly
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi dinakarsoupaty,

This sounds like a homework assignment so let me offer some guidelines.

First off, this is not a good use of recursion.  Still, it can be done.

Second, detecting for the "middle" of the string come in two flavors.  When there are an odd number of characters there is a middle character.  When there are an even number of characters there are not.  Still, this can be handle with simple pointer arithmetic.

To code the function, pass it two parameters.  The address of the first character and the address of the last character.  Within the function check the addresses to make sure that they don't overlap.  i.e. address of the first character must be less than the address of the last character.  (It doesn't really matter this pass, but you'll need it for the recursive calls.  If the addresses overlap, return non-zero, the string is a palindrome.)  Now test the characters.  If they are equal, the function should call itself incrementing the address of the first character and decrementing the address of the second.  If they are not equal, return 0, the string is not a palindrome.

That's about it.


Good Luck!
Kent
0
 
PaulCaswellCommented:
Hi dinakarsoupaty,

Here's a few more hints but you must do the code yourself.

1. A zero-character word is a palindrome.
2. A one-character word is a palindrome.
3. A n-character word is a palindrome if the two end characters are the same and the word without those two characters is a palindrome.

Thats essentially what Kent is saying.

You wll have to accept that this work wont be done for you. I would suggest you post some code and let us help you fix it.

Paul
0
 
Kelvin_KingCommented:
implementation is quite simple.

you word will have n characters, the first being at index 0 and the last at index n-1.

so declare 2 pointers at both ends.

be it: start = 0 , and end = n-1

use a for loop to check is characters at either end are the same or not, some pseudo code.

for loop start here

if( start != end ) return false;

else {
   start ++;
   end --;
   if (start > end || start == end) return true;
}

end for loop here

0
 
PaulCaswellCommented:
Hi dinakarsoupaty,

I see you are having difficulties since you posted another copy of this question here:

http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_21823716.html

You need to post here what difficulties you are having and we can help you. Join in the discussion as it were.

Here's some more help:

Remember my post above?

1. A zero-character word is a palindrome.
2. A one-character word is a palindrome.
3. A n-character word is a palindrome if the two end characters are the same and the word without those two characters is a palindrome.

Something like this may be a good start but you will need to fill in the code:

// Returns 1 if the word is a palindrome.
int IsPalindrome ( char * word, int length )
{
  int is = 0; // Zero means false.
  // 1. A zero-character word is a palindrome.
  // 2. A one-character word is a palindrome.
  if ( /* length is zero or length is one */ )
  {
    /* it IS a palindrome */
    is = 1; // One means true.
  }
  else
  {
    // 3. A n-character word is a palindrome if the two end characters are the same and the word without those two characters is a palindrome.
    if ( /* first character is same as last character */ )
    {
        // Recursively call IsPalindrome to check if the rest of the word is a palindrome.
        is = IsPalindrome ( /* word without first character */ , /* length without first or last character (i.e. length minus two) */ );  
    }
  }
  // Return the result.
  return is;
}

You will get the most help if you post what you have tried here as every time you post, everyone who has posted already gets an e-mail telling them that you have posted. Making a new question will actually reduce your chance of getting help.


Paul
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now