简体   繁体   English

Function in C 检查字符串是否为回文

[英]Function in C to check if a string is a palindrome

The function should check if an input string is a palindrome. function 应该检查输入字符串是否为回文。 It should not be case sensitive, and must ignore every other character except letters and numbers.它不应该区分大小写,并且必须忽略除字母和数字之外的所有其他字符。 The thing I am having problems with is when the string is empty(that means only the spaces are elements of the string), and when the string has a lot of other characters, but no letters or numbers.我遇到的问题是当字符串为空时(这意味着只有空格是字符串的元素),以及当字符串有很多其他字符但没有字母或数字时。

This is my code, and it works well, except in the cases stated above.这是我的代码,除了上述情况外,它运行良好。

  #include <stdio.h>
  #include <string.h>
  #include <ctype.h>

  int is_it_palindrome(const char* str){

    int lenght;
    lenght=strlen(str);

    const char *start=str+0;
    const char *end=str+lenght-1;

    while(start<end){
        if(!isalnum(*start)){
            start++;
        }
        else if(!isalnum(*end)){
            end--;
        }
        else if(toupper(*start)==toupper(*end)){
            start++;
            end--;
        }
        else{
            return 0;
        }
    }

    return 1;
 }

int main() {
    
    printf ("%d", is_it_palindrome("    "));
    printf ("%d", is_it_palindrome("a"));
    printf ("%d", is_it_palindrome(",./!\n+_[]{}@"));
    printf ("%d", is_it_palindrome(",./!\n+_A[]{}@"));
    
return 0;
}

The function returns 0 if it is not a palindrome, and 1 if it is a palindrome.如果 function 不是回文,则返回 0,如果是回文,则返回 1。 So the output here should be 0 1 0 1, but I get 1 1 1 1. I really don't know how to rewrite this program to contain the conditions that I need.所以这里的 output 应该是 0 1 0 1,但我得到的是 1 1 1 1。我真的不知道如何重写这个程序来包含我需要的条件。 I would really appreciate the help.我真的很感激你的帮助。

A string containing no alphanumeric characters is not a palindrome according to your rules yet it doesn't ever reach return 0;根据您的规则,不包含字母数字字符的字符串不是回文,但它永远不会return 0; cause you skip over non alpha numeric characters.导致您跳过非字母数字字符。 To check it you have to add a flag that tracks if you have any alpha numeric characters at all.要检查它,您必须添加一个标记来跟踪您是否有任何字母数字字符。 Additionally to detect the single alnum char I go to start <= end instead of start < end另外为了检测单个 alnum char I go start <= end而不是start < end

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int is_it_palindrome(const char* str) {
    int lenght;
    int has_alnum = 0;
    lenght=strlen(str);

    const char* start = str + 0;
    const char* end = str + lenght - 1;

    while (start <= end) {
        if (!isalnum(*start)) {
            start++;
        } else if (!isalnum(*end)) {
            end--;
        } else if (toupper(*start) == toupper(*end)) {
            has_alnum = 1;
            start++;
            end--;
        } else {
            return 0;
        }
    }

    return has_alnum;
}

int main(void) {
    printf("%d", is_it_palindrome("    "));
    printf("%d", is_it_palindrome("a"));
    printf("%d", is_it_palindrome(",./!\n+_[]{}@"));
    printf("%d", is_it_palindrome(",./!\n+_A[]{}@"));

    return 0;
}

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

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