-1

Write a function that checks whether the string is palindrome. Must use recursive function and ignore spaces. I have done the first part but still not figure out how to ignore space. The following code is what I have tried already.

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>// this is used for strlen
#include <ctype.h>// this is used for isalnum

int checking_palindrome(char *string,int length);
int main()
{
    int result,length;
    char string[] = "a ma ma";
    length = strlen(string);
    result= checking_palindrome(string,length);
    if (result == 1)
        printf("The array is palindrome.\n");
    else
        printf("The array is not palindrome.\n");
    system("pause");
    return 0;
}
int checking_palindrome(char *string, int length)
{
    if (length <= 0)
        return 1;
    else if (string[0] == string[length-1])
        return checking_palindrome(&string[0], length - 2);
    else
        return 0;
}
Dave Nguyen
  • 129
  • 1
  • 1
  • 8
  • 1
    Why are you using `&string[0]` instead of `&string[1]` or `string + 1` as the first argument of `checking_palindrome()`? – MikeCAT Aug 25 '16 at 06:55
  • 2
    Learn how to use a debugger, and how to step through your code line by line while monitoring variables and their values. That would have helped you very quickly. It's also a crucial skill for anyone who wants to take any kind of programming more seriously, even for hobby-programmers. – Some programmer dude Aug 25 '16 at 06:57
  • @MikeCAT actually, that is my mistake. My original code was &string[1]. – Dave Nguyen Aug 25 '16 at 07:32
  • @JoachimPileborg Thank you for your advice. – Dave Nguyen Aug 25 '16 at 07:32

2 Answers2

2

To ignore space, write code to ignore space.

int checking_palindrome(char *string, int length)
{
    if (length <= 0)
        return 1;
    else if (string[0] == ' ')
        /* ignore space at the head of the string */
        return checking_palindrome(&string[1], length - 1);
    else if (string[length-1] == ' ')
        /* ignore space at the tail of the string */
        return checking_palindrome(&string[0], length - 1);
    else if (string[0] == string[length-1])
        /* Respecting original code: &string[0] is used instead of &string[1] for some reason */
        return checking_palindrome(&string[0], length - 2);
    else
        return 0;
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • 1
    Respecting the original code is unwise, since that was one of the flaws in the original code. I have a feeling that OP tested with `amama`, and so reducing the length by 2 results in `ama`. But the code is still wrong. Also, the first `if` could be `if(length<=1)`. – user3386109 Aug 25 '16 at 07:01
  • 1
    Also, putting the equality check earlier, right after the length check will be a win if partial palindromes are more likely than mismatched whitespace. – rici Aug 25 '16 at 08:17
0

After having advice from my tute, it is better to remove spaces from the array and put values to the new array. Then, pass the new array to function.

#include "stdafx.h"//Visual Studio 2015
#include <stdio.h>
#include <stdlib.h>// using for command system("pause") ;
#include <string.h>


int checking_palindrome(char *string, int length);
int main()
{
    int i,j,result, length;
    char string[] = "a ma ma",newstring[50];

    length = strlen(string);
    for(i=0,j=0;i<length;i++)
        if (string[i] != ' ')
        {
            newstring[j] = string[i];
            j++;
        }
    newstring[j] = '\0';
    length = strlen(newstring);

    result = checking_palindrome(newstring, length);
    if (result == 1)
        printf("The array is palindrome.\n");
    else
        printf("The array is not palindrome.\n");
    system("pause");
    return 0;
}
int checking_palindrome(char *string, int length)
{
    if (length <= 0)
        return 1;
    else if (string[0] == string[length - 1])
        return checking_palindrome(&string[1], length - 2);
    else
        return 0;
}
Dave Nguyen
  • 129
  • 1
  • 1
  • 8