Error in Recursive String Palindrome Code

This is a discussion on Error in Recursive String Palindrome Code within the C Programming forums, part of the General Programming Boards category; This has been a long evening, I am trying to get this code to work all night, and all yesterday, ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164

    Error in Recursive String Palindrome Code

    This has been a long evening, I am trying to get this code to work all night, and all yesterday, too.

    Anyway this is what I have:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #define STSIZE 30
    
    void reverse(char *s, int first, int last); 
    void swap(char *p, char *q);
    int maybepalind(char opalinds[], char palinds[]);
    
    int main (void)
    {
    	/*character array declarations*/
    	char palind[STSIZE];
    	char palinds[STSIZE];
    	char opalinds[STSIZE];
    
    	/*variable declarations*/
    	int i = 0;
    	int j = 0;
    	int k = 0;
    	int palans = 0;
    
    	/*banner and prompt for user*/
    	printf("Enter a string of characters.");
    	printf("\nThe computer will determine if the string is a palindrome");
    	printf("\n(spelled the same frontwards and backwards).");
    	printf("\nEnter your string of characters for evaluation: > ");
    	gets(palind);
    	printf("%s\n", palind);
    	int n = strlen(palind);
    
    	/*makes all upper case letters lower case*/
    	for( i = 0; i < n; ++i)
    		if(isupper(palind[i]))
    			palind[i] = tolower(palind[i]);
    
    	/*removes all spaces and punctuation*/
    	while(palind[k] != '\0' && k < n)
    	{
    		if(palind[k] != ' ' && !ispunct(palind[k]))
    		{
    			palinds[j] = palind[k];
    			++j;
    		}
    		++k;
    	}
    	palinds[j] = '\0';			/*terminates new character array*/
    	printf("%s\n", palinds);
    
    	/*copies array to new array for comparison in function below*/
    	char strcpy(char opalinds, char palinds);
    
    	/*function call to reverse letters in character array for comparison*/
    	reverse(palinds, 0, n);
    
    	/*function call to actually do the palindrome comparison on original and reverse string*/
    	/*sets palins equal to the value returned by function*/
    	palans = maybepalind(opalinds, palinds);
    
    	/*if function maybepalind returns '1' the string is a palindrome and that is printed*/
    	/*if the funtions returns a '0' the string is NOT a palindrome and that is printed.*/
    	if (palans = 1)
    		printf("\nThe character string : %s is a PALINDROME!\n", palind);
    	else 
    		printf("\nThe character string : %s is NOT a PALINDROME!\n", palind);
    
    }
    
    /*function to reverse the characters in the string*/
    void reverse(char *s, int first, int last)
    {
    	if (first < last)
    	{
    		swap(&s[first], &s[last]);
    		reverse(s, ++first, --last);
    	}
    }
    
    /*the swap function called by the reverse function*/
    void swap(char *p, char *q)
    {
    	char tmp;
    
    	tmp = *p;
    	*p = *q;
    	*q = tmp;
    }
    
    /*the palindrome check function*/
    int maybepalind(char opalinds[], char palinds[])
    {
    	int first = 0;
    	int ans = 0;
    
    	if(opalinds[first] = '\0')
    		ans = 1;
    	else if (opalinds[first] != palinds[first])
    	{
    		printf("%c  %c\n", opalinds[first], palinds[first]);
    		ans =  0;
    	}
    	else 
    		maybepalind(opalinds[first + 1], palinds[first + 1]);
    	return (ans);
    }
    I get an error that says that the very last line:
    maybepalins(opalinds[first + 1], palinds[first + 1]);
    cannot convert parameter 1 from char to char[].

    I am not sure what that means. I have tried every change I can think of.

    If anyone has any ideas I would appreciate the input. Staring at this code so long I think I am going crosseyed.

    Once I can get it to compile I think it is returning 1 all the time, because the string, no matter what I type in is always a palindrome.

    Thanks Everyone!!!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by clegs View Post
    This has been a long evening, I am trying to get this code to work all night, and all yesterday, too.



    I get an error that says that the very last line:

    maybepalins(opalinds[first + 1], palinds[first + 1]);

    cannot convert parameter 1 from char to char[].

    Try: maybepalins(opalinds+first + 1, palinds+first +1);
    I believe (&opalinds[first + 1], &palinds[first + 1]);
    will also work.

    The compiler wants an address, not an array[something], here.

    I am not sure what that means. I have tried every change I can think of.

    If anyone has any ideas I would appreciate the input. Staring at this code so long I think I am going crosseyed.

    Once I can get it to compile I think it is returning 1 all the time, because the string, no matter what I type in is always a palindrome.

    Thanks Everyone!!!
    If that doesn't work, I'll run it.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164
    Thanks loads that worked. It compiles but now I am back to the error I had before I changed things up.

    My recursive function that checks each elemnt of the string for a match isn't working.

    Every string I enter is a palindrome, so I have something wrong.

    It should fist check that the first element of either string is not the end of string.
    If that is false, then it compared the same element of each string to see if they are not a match; if they don't match, then the string isn't a palindrome and a 0 is returned to main.
    If they do match, then the final else increments both strings to the next element.

    THat isn't working.

    Is ther eanything glaring that you can see where my code doesn't match my algorithm.

    Thanks again for the last fix. I was losing hair over that one.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Notes:

    char strcpy(char array1, char array2); is wrong. Remove the "char" (all three of them).

    if(palin=1), should be changed to if(palin==1)

    if(opalinds(first) = '\0'; also needs two equal signs in a row, not just one.

    Two other problems:

    1) n = strlen(), is one char too long, because it's including the EOS char '\0'. Decrement n by one, before you pass it as a parameter to reverse(), and that fixes a major problem.

    2) In maybepalind(), change the int variable ans to static int ans. Now the ans at the last recursion, is kept as you
    pop back through the recursion.

    and it works.


    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #define STSIZE 30
    
    void reverse(char *s, int first, int last); 
    void swap(char *p, char *q);
    int maybepalind(char opalinds[], char palinds[]);
    
    int main (void)
    {
    	/*character array declarations*/
    	char palind[STSIZE];
    	char palinds[STSIZE];
    	char opalinds[STSIZE];
    
    	/*variable declarations*/
       int n = 0;
    	int i = 0;
    	int j = 0;
    	int k = 0;
    	int palans = 0;
    
    	/*banner and prompt for user*/
    	printf("Enter a string of characters.");
    	printf("\nThe computer will determine if the string is a palindrome");
    	printf("\n(spelled the same frontwards and backwards).");
    	printf("\nEnter your string of characters for evaluation: > ");
    	gets(palind);
    	printf("%s\n", palind);
    	n = strlen(palind);
    
    	/*makes all upper case letters lower case*/
    	for( i = 0; i < n; ++i)
    		if(isupper(palind[i]))
    			palind[i] = tolower(palind[i]);
    
    	/*removes all spaces and punctuation*/
    	while(palind[k] != '\0' && k < n)
    	{
    		if(palind[k] != ' ' && !ispunct(palind[k]))
    		{
    			palinds[j] = palind[k];
    			++j;
    		}
    		++k;
    	}
    	palinds[j] = '\0';			/*terminates new character array*/
    	printf("%s\n", palinds);
    
    	/*copies array to new array for comparison in function below*/
    	strcpy(opalinds, palinds);
    
    	/*function call to reverse letters in character array for comparison*/
    	reverse(palinds, 0, n-1);
    
    	/*function call to actually do the palindrome comparison on original and reverse string*/
    	/*sets palins equal to the value returned by function*/
    	palans = maybepalind(opalinds, palinds);
    
    	/*if function maybepalind returns '1' the string is a palindrome and that is printed*/
    	/*if the funtions returns a '0' the string is NOT a palindrome and that is printed.*/
    	if (palans == 1)
    		printf("\nThe character string : %s is a PALINDROME!\n", palind);
    	else 
    		printf("\nThe character string : %s is NOT a PALINDROME!\n", palind);
    
       printf("\n\n\t    Program Complete - Press Enter to Continue\n");
       getchar();
       return 0;
    }
    
    /*function to reverse the characters in the string*/
    void reverse(char *s, int first, int last)
    {
    	if (first < last)
    	{
    		swap(&s[first], &s[last]);
    		reverse(s, ++first, --last);
    	}
    }
    
    /*the swap function called by the reverse function*/
    void swap(char *p, char *q)
    {
    	char tmp;
    
    	tmp = *p;
    	*p = *q;
    	*q = tmp;
    }
    
    /*the palindrome check function*/
    int maybepalind(char opalinds[], char palinds[])
    {
    	int first = 0;
           static int ans = 0;
    
    	if(opalinds[first] == '\0')
    		ans = 1;
    	else if (opalinds[first] != palinds[first])
    	{
    		printf("%c  %c\n", opalinds[first], palinds[first]);
    		ans =  0;
    	}
    	else 
    		maybepalind(&opalinds[first + 1], &palinds[first + 1]);
    	return (ans);
    }
    Last edited by Adak; 12-21-2008 at 08:15 AM.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164
    Adak,


    Thank you so much for your help. I stopped to get a few hours of sleep and now I am back at it.

    I stepped through your code and compared what you have (and said to change), with the code I have and I made the changes, but unfortunately, it still doesn't work for me.

    Now, I have "not a palindrome" returned every time.

    I used - Christmas and Madam I'm Adam.
    While Christmas isn't a palindrome, Madam I'm Adam is, but both are identified as not a palindrome.

    I appreciate your input here, and understand, now where I went wrong, but...it is still wrong when I test it.

    I even did a copy and paste to a different file, of your entire code, and I get the same result.

    I am really stumped here.

  6. #6
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    494

    Now, I have "not a palindrome" returned every time.

    I used - Christmas and Madam I'm Adam.
    While Christmas isn't a palindrome, Madam I'm Adam is, but both are identified as not a palindrome.
    The string "Madam I'm Adam" is a NOT a palindrome technically but maybe a palindrome literally because you have spaces and single quote character in the middle which would make the phrase different if it is read backwards.
    One possible solution is to incorporate the additional checks in your logic so that spaces and special characters are ignored from your input.
    Edit:
    Ah!The code seems to have such logic already it seems.
    Last edited by stevesmithx; 12-21-2008 at 10:41 AM.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  7. #7
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    494
    I think something goes wrong in your reverse() function.
    It surprises me that program even proceeds to completion without a return statement
    in the recursive reverse function.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  8. #8
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164
    Yea! I have both up in main().

    I didn't think this problem was going to cause this much grief. It HAS to be somethign really simple. I was hoping another pair, or two, of eyes would help.

    Thanks again for all the input.

  9. #9
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164
    It is becasue I have a pointer to the array in the argument list.

    It handles the return for me.

  10. #10
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    494
    No worries!.
    I found the problem.
    The devil is in this line:
    Code:
    reverse(palinds, 0, n-1);
    Look closely at what n is.

    Code:
    n=strlen(palind)
    and NOT

    Code:
    n=strlen(palinds)
    Whoa!Another reason to have meaningful and distinguishable variable names!
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  11. #11
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    494
    obviously length of palinds is NOT equal to that of palind as you're stripping punctuations from there.
    Also any good reason you are using gets()?
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  12. #12
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164
    I changed my reverse function call to read:

    /*function call to reverse letters in character array for comparison*/
    reverse(palinds, 0, strlen(palinds)-1);

    And I still get everything evaluated as NOT a palindrome.

    There has to be something small in the recursive function that returns 1 or 0, that is causing my grief. But, for the life of me, I can't find it. Even after 3 hours of sleep. ARGGGGG!

  13. #13
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164
    Whoops!!!

    I just got an "IS PALINDROME"return.

    let me check it out a few more times...

    There may be hope...

  14. #14
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164
    YAHOOO!!!!

    IT all works Now!! MY CODE WORKS!

    Thanks to you both. It is NOW a working program.

    I can go back to sleep before starting the next assignment.

    My Brain thanks you profusely!

    Thank you, Thank YOU!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 11:47 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  5. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM

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