简体   繁体   English

C:检查字符串是否是回文

[英]C:Checking whether the string is Palindrome

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM