1. ## palindrom using recursion?

Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int is_palindrom(char niz[]);

int main(int argc, char *argv[])
{
if(argc<2)
{
printf("Need an argument-a word!\n");
exit(1);
}

if(strlen(argv[1])==0 || strlen(argv[1])==1)
{ printf("You have to input a word ,not a character!\n");
exit(1);
}

if(is_palindrom(argv[1])==0)
{
printf("A word %s is palindrom!\n",argv[1]);
}

else printf("A word %s is not a palindrom!\n",argv[1]);

}

int is_palindrom(char niz[])
{
char reverse_niz[strlen(niz)];
int i;
int x=0;
for(i=strlen(niz)-1;i>=0;i--)
{
reverse_niz[x]=niz[i];
x++;
}
reverse_niz[x]='\0';

if(strcmp(reverse_niz,niz)==0)
return 0;

else

return 1;

}
The above program works. A palindrom is a a word which can be read backwards the same as forward(ex. cepec). In the above program i first reversed the inputed word and stored it into reverse_niz and then compare that string to the original. And if the words are the same the thing is palindrom.

BUT I have to implement that function is_palindrom(char niz) using recursion. Somehow I have to make for the method to be callig itself in the body of a method.

2. Do you understand the concept of recursion?
Code:
int factorial(int n)
{
if( n < 2 ) return 1;
return n * factorial(n-1);
}
There's a classical example of using recursion. As you can see, it's split into two parts. The first line, the base case, is essential. That line ensures that the factorial function ends eventually.

For your program, the base case isn't difficult. If you have a string one or fewer characters long, it's vacuously true that it's a palindrome.

The second part is a bit more difficult. Thinking recursively takes practice.

For your program, this part of the function will do essentially the same thing as the body of your loop does: check if one char is equal to another char.

Now you just need to put this algorithm into code, making sure that it doesn't end up calling itself endlessly.

4. Then start with a simpler one. For instance, how about trying to write a recursive function to print the numbers 1 to n?

5. so if n is 3 the output would be 6?

6. no if n is 3 it would print
1 2 3

7. Originally Posted by joshdick
Then start with a simpler one. For instance, how about trying to write a recursive function to print the numbers 1 to n?
Code:
int count (int n) {
int i=1;
int num=count(n-n+i);
while(num<=n)
{

num=count(n-n+i);
printf("%d\n",num);
i++;
}

}
Maybe like this? I will give it a try.

8. Did you notice how the factorial function I provided worked without incrementing a counter or using a while loop? You don't need those things in your recursive function.

Also, (n-n+i) is just i.

9. Code:
int count (int n) {
if(n>=1)
return n - count(n-1);
}
Maybe like this?
Any good tutorials about recursion,cause this is a bit new to me.

10. Start here

- Micko

11. thanks micko!

12. You're welcome, later when you learn a little more about recursion come back. I have interesting recursive solution of your problem but I'll wait and see how you will handle this.

- Micko

13. "I have discovered a truly marvelous demonstration of this proposition that this margin is too narrow to contain."

14. I am on it. Ill be back!

btw: Thantos, I meant the factorial function. The result is 6 if n =3.

15. I get a segmentation fault error on this one. Im trying to do that count thing from 1 to n. It looks ok to me . What did I do wrong?

Code:
#include<stdio.h>
int count(int n);
int main()

{
int z=3;
printf("%d\n", count(z));

return 0;
}

int count(int n) {
int i=1;
while(i<=n) {
return  count(n)-count(n-i);
i++;
}
return 1;

}