Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,671 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,184 people online right now. Registration is fast and FREE... Join Now!




Palindrome thru recursion

 
Reply to this topicStart new topic

Palindrome thru recursion

naash
post 5 Sep, 2007 - 07:00 AM
Post #1


D.I.C Head

**
Joined: 25 Aug, 2007
Posts: 77


My Contributions


1. Write a recursive for that determines if a string is palindrome. If it is, print the string le one line followed by “is a particular “or “is not a palindrome” as the case may be. Ignore spaces and case.


I have done this by iteration....here's my logic

CODE

char s[10];
int flag=1;
for(i=0;i<l;i++)
  if(s[i]!=s[l-i-1])
     {
        flag=0;
        break;
     }
if(flag==0)
   cout<<"Not a palindrome";
if(flag!=0)
  cout<<"Palindrome";


But recursion puzzles me. Now recursion requires a testing cond^n and stopping cond^n.

Can anybody tell me what these 2 conditions should be so that I can start off with programming using recursion ?

This post has been edited by naash: 5 Sep, 2007 - 07:02 AM
User is offlineProfile CardPM

Go to the top of the page

Martyr2
post 5 Sep, 2007 - 08:24 AM
Post #2


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 5,062



Thanked 175 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


Your test condition will be something along the lines of...

CODE

if (str[0] == str[length-1]) {                        
      return isPalindrome (str+1, length-2);
}
else {
      return 0; // not a palidrome
}


And your stop condition will be along the lines of if (length<1) return 1; if it is a palidrome otherwise your else in the if condition above will return 0 if it is not a palidrome.

Hope this gives you the push you needed. smile.gif
User is offlineProfile CardPM

Go to the top of the page

naash
post 5 Sep, 2007 - 05:33 PM
Post #3


D.I.C Head

**
Joined: 25 Aug, 2007
Posts: 77


My Contributions


QUOTE(Martyr2 @ 5 Sep, 2007 - 09:24 AM) *

Your test condition will be something along the lines of...

CODE

if (str[0] == str[length-1]) {                        
      return isPalindrome (str+1, length-2);
}
else {
      return 0; // not a palidrome
}


And your stop condition will be along the lines of if (length<1) return 1; if it is a palidrome otherwise your else in the if condition above will return 0 if it is not a palidrome.

Hope this gives you the push you needed. smile.gif



Thank you so much it worked !!! smile.gif
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 06:02AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month