nontype
nontype

Reputation: 25

checking non symmetrical Palindrome

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);
    }
}

Upvotes: 0

Views: 64

Answers (1)

Somil  Garg
Somil Garg

Reputation: 474

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);

}

Upvotes: 1

Related Questions