Help with recursion program

This is a discussion on Help with recursion program within the C Programming forums, part of the General Programming Boards category; The program is supposed to test whether or not a word is a palindrome through using recursion. I'm getting a ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    24

    Help with recursion program

    The program is supposed to test whether or not a word is a palindrome through using recursion. I'm getting a lot of errors when i try and compile this program. Can anyone help me out with this? It would be much appreciated.
    Code:
    #include<stdio.h>
    int palin();
    int palin(char array[20], int n)
    {
     int *p, *q;
     q = n;
     p = q;
     p = &array[0];
     printf("Enter a string (20 characters max): ");
     gets(array[]); 
     
     
    
     if (n <= strlen(array)/2)
    
       if (array[n] == array[strlen(array)-n-1])
         return palin(array, n+1);
       else return 0;
    }
    
    int main()
    {
     int i;
     i = palin(array, n);
     if ( i!= 0) printf("Is a palindrome");
     else printf("Is not a palindrome"); 
     return 0 ;
    }
    *edit* I really need this programming project done and I'm not going to be able to finish it by 12 tonight when it's due. I would be willing to pay someone to help me with the program. I have to develop 5 different functions that require recursion. If anyone is interested in helping me with this send me a pm.
    Last edited by nafix; 11-02-2007 at 03:56 PM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,259
    To address the compilation errors, you need to actually declare and initialise variables within main() that you pass as arguments to palin(). It is a good idea to take out the prototype "int palin();" beforehand as well.

    As to the working of your function, that still needs a bit of work. You would be better off reading the string, and passing it to your function, rather than reading it within the function (particularly if the function is recursive).

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You also need to find a way to return "non-zero" when you have a palindrome. Currently, the function either returns the result of recursion or zero. That can't lead to anything other than zero being returned by your return statements. [You may also want to put braces behind your if- and else-statements, at least when they are more than a single line, so that it's clear what belongs inside which part of the if/else].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    24
    So here is my new code:

    Code:
    #include<stdio.h>
    #include<string.h>
    int palin(char array[20])
    {
     char *n;
     n = array[0];
    
     if (n <= strlen(array)/2)
       {
       if (array[n] == array[strlen(array)-n-1])
         return palin(array[n+1]);
       else return 0;
       } 
    }
    
    int palin_main()
    {
     int i;
     char array[20];
     printf("Enter a string (20 characters max): ");
     gets(array); 
     i = palin(array);
    
       if ( i!= 0) printf("Is a palindrome");
       else printf("Is not a palindrome"); 
     return 0;
    }
    These are the errors I get when I try to compile. How can I get n to be at 0 of the array?
    a.c: In function `palin':
    a.c:6: warning: assignment makes pointer from integer without a cast
    a.c:8: warning: comparison between pointer and integer
    a.c:10: error: array subscript is not an integer
    a.c:10: error: invalid operands to binary -
    a.c:11: error: array subscript is not an integer

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    24
    Does anyone know why my pointer isn't working for this program?

  6. #6
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Code:
    #include<stdio.h>
    #include<string.h>
    int palin(char array[20])
    {
     char *n;
     n = array[0];
     /* Line above is incorrect; (char *) = (char)
     
    
     if (n <= strlen(array)/2)
     /* I'm not sure to understand what you are doing.
        strlen return the lenght of a string */
       {
       if (array[n] == array[strlen(array)-n-1])
       /* This doesn't look correct either */
         return palin(array[n+1]);
    
       else return 0;
       } 
    }
    
    int palin_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...
    Last edited by foxman; 11-03-2007 at 04:39 PM. Reason: Wrote n - 1; was in fact n - 2

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    24
    [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.

  8. #8
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    I think we need to clarify some things...

    1.
    Code:
    char *n;
    n = array[0];
    Is not correct. You declare n as a 'char *' (pointer on a character), but the variable 'array[0]' is a char. If you want n to store the value of the character array[0], then, declare n as 'char', not 'char *'.


    2.
    Code:
    if (n <= strlen(array)/2)
    So... in this case.. let's say you want n to be the first character of the array (array[0]). What you are doing is comparing a character with an integer. You are looking if the first character has a numerical value less than the number of character, divided by 2. It's total non-sense. Even if n would store an adress to a character (being a char *), it still would be total non sense, since comparing an adress with an integer, well, i hope you understand this isn't correct....


    3.
    Code:
    if (array[n] == array[strlen(array)-n-1])
    Again, this looks incorrect. What is your variable n for again ? This is everything but sementically good.


    That's it. You'll never be able to find if a string is a palindrome or not by having a function prototype with only one argument (you could but you would have to change the value of the string, which is normally a think you don't want to). You need at least one more parameter (an integer) saying "consider this as the lenght of the string".

    Rethink your code... or maybe you should relook your books... and if you would like to see a working solution, i could give you one, if you think you'll learn more from it...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21