0

I've written a program for a myprogramminglab assignment that is supposed to take a string input, check if its a palindrome, and output the result.

What I have is working for everything but one test. "a man a plan a canal panama" is a palindrome but my program is saying its not. I think that it must be because the program compares the second blank space with the 'm' in 'panama'.

On the flip side, "able was i ere i saw elba" is caught as a palindrome but I would guess that this is because it is symmetrical and skipping a white space on the left happens at the same time as skipping a white space on the right.

#include <stdio.h>
#include <stdlib.h>

int is_not_alnum(char c);
int testPalindrome(const char string[], int begin, int end);


int main (void)
{
    int n;
    char temp;


    printf("Enter the size of your string: ");

    scanf("%d" , &n);

    char string[n];

    printf("Enter your string to check if it is a palindrome: ");
    scanf("%c",&temp);
    scanf("%[^\n]" , string);

    if(testPalindrome(string, 0 , n - 1))
        printf("\"%s\" is a Palindrome.\n" , string);
    else
        printf("\"%s\" is not a Palindrome.\n" , string);


    //puts("");

    return(0);
}

//program only needs to worry about lowercase and 0-9 alphanumeric
int is_not_alnum(char c)
{
    if( ((c >= 'a') && (c <= 'Z')) || ((c >= '0') && (c <= '9')) )
        return(0);
    else
        return(1);
}

int testPalindrome(const char string[], int begin, int end)
{
    while (begin <= end)
    {
    if(is_not_alnum(string[begin]))
        begin++;
    if(is_not_alnum(string[end]))
        end--;

    if(string[begin] == string[end])
    {
        return(testPalindrome(string , begin++ , end--));
        return(1);
    }
    else
        return(0);
    }
}

nontype
  • 25
  • 6
  • You should enable warnings and read them. Compile with `-Wall` – klutt Oct 16 '19 at 02:59
  • Possible duplicate of [C recursive function palindrome](https://stackoverflow.com/questions/46558406/c-recursive-function-palindrome) – klutt Oct 16 '19 at 03:17
  • Why not remove all space before you check? (you can copy to a separate buffer, just skip copying the spaces) – David C. Rankin Oct 16 '19 at 04:06

1 Answers1

1

You are checking for the uppercase character 'Z' it should be:

#include <stdio.h>
#include <stdlib.h>

int is_not_alnum(char c);
int testPalindrome(const char string[], int begin, int end);


int main (void)
{
    int n;
    char temp;


    printf("Enter the size of your string: ");

    scanf("%d" , &n);

    char string[n];

    printf("Enter your string to check if it is a palindrome: ");
    scanf("%c",&temp);
    scanf("%[^\n]" , string);

    if(testPalindrome(string, 0 , n - 1))
        printf("\"%s\" is a Palindrome.\n" , string);
    else
        printf("\"%s\" is not a Palindrome.\n" , string);


    //puts("");

    return(0);
}

//program only needs to worry about lowercase and 0-9 alphanumeric
int is_not_alnum(char c)
{
    if( ((c >= 'a') && (c <= 'z')) || ((c >= '0') && (c <= '9')) )
        return(0);
    else
        return(1);
}

int testPalindrome(const char string[], int begin, int end)
{
    if (begin <= end)
       return(1);

    if(is_not_alnum(string[begin]))
        begin++;
    if(is_not_alnum(string[end]))
        end--;

    if(string[begin] == string[end])
    {
        return(testPalindrome(string , begin++ , end--));
    }
    else
        return(0);

}
Somil Garg
  • 474
  • 4
  • 12
  • 1
    so oddly enough I fixed that mistake but now I get a segmentation fault (core dump). Not sure how changing 'Z' to 'z' makes my program try to access memory that is out of bounds. – nontype Oct 16 '19 at 04:23
  • You need to fix the recursion there is no need of the while loop over there. I have updated the code . Your current code keeps on checking only one condition. – Somil Garg Oct 16 '19 at 09:08