[Rewriting questions] 125. Verify palindrome string-Given a string, verify whether it is a palindrome string, only consider letters and numbers, and ignore the case of letters.

Topic: 125. Verify palindrome

Given a string, verify whether it is a palindrome, consider only letters and numbers, and ignore the case of letters.
Insert picture description here

answer:

bool isPalindrome(char * s)
{
    
    
    // 左指针
    char* left=s;
    // 右指针
    char* right=s;
    // 使得右指针指向最后一个字母
    int count=strlen(s)-1;
    while(count--)
    {
    
    
        right++;
    }
    while(left<right)
    {
    
    
        // 左指针找到字母或数字字符
        while(left<right && !isalnum(*left))
        {
    
    
            left++;
        }
        // 右指针找到字母或数字字符
        while(left<right && !isalnum(*right))
        {
    
    
            right--;
        }
        // 如果左指针为大写字母,转化为小写字母
        if(*left>='A'&&*left<='Z')
        {
    
    
            *left+=32;
        }
        // 如果右指针为大写字母,转化为小写字母
        if(*right>='A'&&*right<='Z')
        {
    
    
            *right+=32;
        }
        // 左右指针进行对比
        if(*left!=*right)
        {
    
    
            return false;
        }
        left++;
        right--;
    }
    return true;
}

Guess you like

Origin blog.csdn.net/m0_46613023/article/details/113951734