473,394 Members | 1,845 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Palindromes with recursion

Hi there,

Just need some help with this small program.

Using recursion I'm trying to find out if a word (in this case a test word) is a palindrome or not. If it is it should return a 1 to result, but instead it shows a big number that looks like a memory address.

Any guide will be deeply apreciated.



# include <stdio.h>
# include <process.h>
# include <string.h>

//Function prototype

int palindrome(char w[],int l,int r);


int
main (void)

{
int left=0;
int right=3;
int result=0;

char word[5]="deed"; //this is a test to check the function


result = palindrome (word,left,right);

printf("The word is %d\n",result ); //if the word is 1 its a palindrome
system("pause"); //if its 0 its not

return (0);
}


//palindrome function


int palindrome (char w [],int l, int r) //get parameters
{
if (r<=l) //base case 1 :we reached the end without problem
{
return (1);
}
else if (w [l] != w [r]) //base case 2 : the letters are not correct
{
return (0);
}
else
{
palindrome (w, (l+1),(r-1)); //recursive formula
}

}
Aug 1 '08 #1
4 10933
boxfish
469 Expert 256MB
Hi,
The initial call to your recursive function does not return a value, so your result variable is uninitialized. Your palindrome function returns 1 if it's finished and the word is a palindrome, 0 if the word is discoverd not to be a palidrome, or it does not return anything. change
Expand|Select|Wrap|Line Numbers
  1. palindrome (w, (l+1),(r-1)); //recursive formula
to
Expand|Select|Wrap|Line Numbers
  1. return palindrome (w, (l+1),(r-1)); //recursive formula
Hope this helps.
Aug 2 '08 #2
Thanks very much ! It worked perfectly !
Aug 2 '08 #3
boxfish
469 Expert 256MB
I'm glad it worked. However, it seems your code does not work when the palindrome has an odd number of letters.
Aug 2 '08 #4
I'll check that , thanks again.
Aug 4 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Peri | last post by:
I'm trying to create Python parser/interpreter using ANTLR. Reading grammar from language refference I found: or_expr::= xor_expr | or_expr "|" xor_expr For me it looks like infinite recursion....
12
by: da Vinci | last post by:
Greetings. I want to get everyone's opinion on the use of recursion. We covered it in class tonight and I want a good solid answer from people in the "know" on how well recursion is accepted...
43
by: Lorenzo Villari | last post by:
I've tried to transform this into a not recursive version but without luck... #include <stdio.h> void countdown(int p) { int x;
75
by: Sathyaish | last post by:
Can every problem that has an iterative solution also be expressed in terms of a recursive solution? I tried one example, and am in the process of trying out more examples, increasing their...
4
by: elsa | last post by:
hi everybody..i need to define two functions that test for word Palindromes.. 1- void bool isPalindromeUsingIteration(string ) that takes a string as a paremeter and tests if it is a Palindrome...
20
by: athar.mirchi | last post by:
..plz define it.
0
by: 249740 | last post by:
A palindrome is a number/string that reads the same backwards and forward. For example: BOB, 3113 etc. Write a program that reads in an input value or string and outputs the number of possible...
35
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.