[QUOTE=foxman;684487]
Code:
#include<stdio.h>
#include<string.h>
int palin(char array[20])
{
char *n;
n = array[0];
/* I don't know what u mean with (char *) = (char). How exactly would i code that?
/* here i'm dividing the length of the array by 2, then comparing the first char of the array with its opposite char in the array, if they match then it moves to the next char*/
if (n <= strlen(array)/2)
strlen return the lenght of a string */[/COLOR]
{
if (array[n] == array[strlen(array)-n-1])
return palin(array[n+1]);
else return 0;
}
}
int main() // Hum, function name should be main
{
int i;
char array[20];
printf("Enter a string (20 characters max): ");
/* In fact, if the user enter 20 characters, you'll have buffer overflow */
gets(array);
/* You shouldn't use gets; use something like fgets(array, 20, stdin),
or better, fgets(array, sizeof(array) / sizeof(*array), stdin) */
i = palin(array);
if ( i!= 0) printf("Is a palindrome");
else printf("Is not a palindrome");
return 0;
}
Think about rewriting your palindrome function prototype to something like
Code:
int palin(char *array, int n);
where n is the number of characters in the string, and make recursive call like
Code:
palin(array + 1, n - 2);
By the way, normal recursive function always have a similar pattern. First, you test if you'll converge by calling recursively the function, then you check if you are in a "base case", else you call your function recursively with a smaller set than the original call... for example, in your case...
well in fact i won't post an example since it will mostly give you the answer...[/QUI explained what I'm trying to do with the recursion. Could you clarify the part about the pointer? Also I'm going to avoid fgets because we haven't learned that yet.