0

I'm trying to write a function that receives a string and I need to check if it's a palindrome. In addition the function transfers the index's of the start and the end of the palindrome. Assuming the string is not a palindrome and it has a mini palindrome so there's only that (the mini palindrome). My problem is that my function isn't ignoring spaces between words. If that wasn't clear enough then an example would be for the input "My gym" the function should transfer the numbers high=5, low=0 and return the number 1. Another example for the input "anana" it should transfer low=1 high=5 and return 1. Also there is no difference between lower/uppercase letters.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
int f5(const char* str, int n, int* low, int* high)
{
    int i, left, right;
    for (i=2; i < n; i++)
    {
        if (tolower(str[i]) == tolower(str[i - 1]))
        {
            left = i - 1;
            right = i;
            break;
        }
        else if (tolower(str[i]) == tolower(str[i - 2]))
        {
            left = i - 2;
            right = i;
            break;
        }
    }
    if (i == n)
        return 0;
    while ((left>=0)&& (right<=n-1))
    {
        if (tolower(str[left]) != tolower(str[right]))
        {
            return 0;
        }
        else {
            left--;
            right++;
        }
    }
    *low = left;
    *high =right;
    return 1;

    


}



void main()
{
    int low, high;
    char str[] = "my gym";
    int num = f5(str, strlen(str), &low, &high);
    if (num == 1)
    {
        printf("The function is palindrome that start with index %d and finish with %d", low, high);
    }
    else {
        printf("The function is not palindrome");
    }
}

the output for this specific code is "The function is not a palindrome" when it should be a palindrome

  • 1
    Please [edit] and show some examples if strings and expected and actual outputs. – Jabberwocky Jan 10 '21 at 18:29
  • 1
    the space between "my" and "gym" is your issue, you should either skip over spaces or remove spaces upon first receiving input. – Coder Jan 10 '21 at 18:37
  • 3
    Does this answer your question? [Removing Spaces from a String in C?](https://stackoverflow.com/questions/1726302/removing-spaces-from-a-string-in-c) – Coder Jan 10 '21 at 18:41
  • @JohnD he should just skip the spaces. Removing the space would cause the `high` of "My gym" to be 4 – anotherOne Jan 10 '21 at 19:50

0 Answers0