[Question solution] verify palindrome

Title requirements
Given a string, verify whether it is a palindrome, only consider letters and numbers, and ignore the capitalization of letters.
Explanation: In this question, we define an empty string as a valid palindrome string.

Example 1:

输入: "A man, a plan, a canal: Panama"
输出: true

Example 2:

输入: "race a car"
输出: false

Problem-solving ideas
1. This question requires judging whether the string is a palindrome. The meaning of the palindrome is that the string is read from the beginning and from the end (just like the abstract).
2. As the title explains, the null character is also a valid palindrome string, so we have to judge whether the string is empty later.
3. Here is the difference between NULL and empty characters:

  • NULL means that an empty object is declared and can be assigned to any object.
  • However, the empty character occupies an empty character space and represents an object instance.

4. We will use three additional functions

  • strlen() is used to retrieve the length of the string, the header file <string.h>;
  • isalnum() is used to determine whether the character is a number or letter, if it is, it returns 0, otherwise it returns 1;
  • tolower() converts letters to lowercase, the header files of isalnum() and tolower() are both <ctype.h>

Problem solving process

  1. After receiving the data, we must first determine the legality of the data
  2. Judge whether the pointed character is a letter or number, if yes, proceed to the next judgment. Otherwise, the loop variable changes, skip this judgment and proceed to the next judgment.
  3. When the head and tail pointers are letters or numbers, you can start to compare whether they are the same. If it is the same, the loop variable is changed and the next loop is performed. Otherwise, return 0 (false) and exit the loop (representing that the string is not a palindrome)
  4. If the loop does not exit, all execution is complete. This means that the string is a palindrome and can return 1 (true)

The code is submitted in Likou compiler

bool isPalindrome(char * str){
    
    
    int i, n;

    //1、判断数据合法性
    if(str == NULL)
        return 0;
    else if(strlen(str) == 0)
        return 1;

    n = strlen(str) - 1;

    for(i = 0; i < n;){
    
    
    //2、判断字符是否为数字或字母,否则跳过
        if(!isalnum(str[i])){
    
       //头部判断,并向后移
            i++;
            continue;
        }
        if(!isalnum(str[n])){
    
       //尾部判断,并向前移
            n--;
            continue;
        }

    //3、如果字符为数字或字母,则判断是否相同。否则返回0(退出程序)
        if(tolower(str[i]) != tolower(str[n]))
            return 0;
        //更新循环条件
        i++;
        n--;
    }
    //4、当循环完成时,代表字符串为回文串
    return 1;
}

Here to explain, because there is no bool type in C language, we can use 0 or 1 instead.


The native compiler type is called by the main() function. In fact, it just adds the header file and the return value to judge the output.

#include <stdio.h>
#include <string.h>    //为函数strlen() 提供原型
#include <ctype.h>    //为函数isalnum() 和tolower() 提供原型

int isPalindrome(char* str);

int main(void){
    
    
    char* str = "A man, a plan,x a canal: Panama";

    if(isPalindrome(str))
        printf("%s", "true");
    else
        printf("%s", "flase");

    return 0;
}

int isPalindrome(char* str){
    
    
    int i, n;

    //1、判断数据合法性
    if(str == NULL)
        return 0;
    else if(strlen(str) == 0)
        return 1;

    n = strlen(str) - 1;

    for(i = 0; i < n;){
    
    
    //2、判断字符是否为数字或字母,否则跳过
        if(!isalnum(str[i])){
    
       //头部判断,并向后移
            i++;
            continue;
        }
        if(!isalnum(str[n])){
    
       //尾部判断,并向前移
            n--;
            continue;
        }

    //3、如果字符为数字或字母,则判断是否相同。否则返回0(退出程序)
        if(tolower(str[i]) != tolower(str[n]))
            return 0;
        i++;
        n--;
    }
    //4、当循环完成时,代表字符串为回文串
    return 1;
}

Update progress this month 3/15

Creation is not easy, your likes are my biggest motivation! ! !
See you next time end~

Guess you like

Origin blog.csdn.net/weixin_43776724/article/details/106874272