Valid Palindrome 判断一个字符串是否是回文串

class Solution {
public:
    bool isValidchar(char c)
    {
        if(c>='a'&&c<='z') return true;
        if(c>='A'&&c<='Z') return true;
        if(c>='0'&&c<='9') return true;
        else return false;
    }
    
    char lowercase(char c)
    {
        if(c>='A'&&c<='Z') return c-'A'+'a';
        else return c;
        
    }
    
    
    bool isPalindrome(string s) {
        int l=0;
        int r=s.size()-1;
        while(l<s.size()&&!isValidchar(s[l]))
        l++;
        while(r>=0&&!isValidchar(s[r]))
        r--;
        while(l<r)
        {
            if(lowercase(s[l])==lowercase(s[r]))
            {
            l++;
            while(l<s.size()&&!isValidchar(s[l]))
            l++;
            r--;
            while(r>0&&!isValidchar(s[r]))
            r--;
            }
            else return false;
            
        }
        return true;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我来为您解答这些问题。 1. C语言中删除字符串中的重复字符可以通过以下代码实现: ```c #include <stdio.h> #include <string.h> void removeDuplicates(char str[]){ int len = strlen(str); int index = 0; for(int i=0; i<len; i++){ int j; for(j=0; j<i; j++){ if(str[i] == str[j]){ break; } } if(j == i){ str[index++] = str[i]; } } str[index] = '\0'; } int main() { char str[100]; printf("Enter a string:\n"); fgets(str, 100, stdin); removeDuplicates(str); printf("String after removing duplicates: %s", str); return 0; } ``` 2. C语言中判断一个数是否为回文数可以通过以下代码实现: ```c #include <stdio.h> #include <stdbool.h> bool isPalindrome(int num){ int temp = num; int reverse = 0; while(temp != 0){ reverse = reverse * 10 + temp % 10; temp /= 10; } return num == reverse; } int main() { int num; printf("Enter a number:\n"); scanf("%d", &num); if(isPalindrome(num)){ printf("%d is a palindrome number.", num); }else{ printf("%d is not a palindrome number.", num); } return 0; } ``` 3. C语言中统计字符串中单词的个数可以通过以下代码实现: ```c #include <stdio.h> #include <string.h> #include <ctype.h> int countWords(char str[]){ int count = 0; int len = strlen(str); for(int i=0; i<len; i++){ if(isspace(str[i]) || ispunct(str[i])){ count++; } } return count; } int main() { char str[100]; printf("Enter a string:\n"); fgets(str, 100, stdin); int count = countWords(str); printf("Number of words in the string: %d", count); return 0; } ``` 4. C语言中判断一个字符串是否为合法的C标识符可以通过以下代码实现: ```c #include <stdio.h> #include <string.h> #include <stdbool.h> #include <ctype.h> bool isValidIdentifier(char str[]){ int len = strlen(str); if(!isalpha(str[0]) && str[0] != '_'){ return false; } for(int i=1; i<len; i++){ if(!isalnum(str[i]) && str[i] != '_'){ return false; } } return true; } int main() { char str[100]; printf("Enter a string:\n"); fgets(str, 100, stdin); if(isValidIdentifier(str)){ printf("%s is a valid C identifier.", str); }else{ printf("%s is not a valid C identifier.", str); } return 0; } ``` 5. C语言中密码翻译可以通过以下代码实现: ```c #include <stdio.h> #include <string.h> void translatePassword(char password[]){ int len = strlen(password); for(int i=0; i<len; i++){ if(password[i] >= 'a' && password[i] <= 'z'){ password[i] = 'a' + 'z' - password[i]; }else if(password[i] >= 'A' && password[i] <= 'Z'){ password[i] = 'A' + 'Z' - password[i]; } } } int main() { char password[100]; printf("Enter a password:\n"); fgets(password, 100, stdin); translatePassword(password); printf("Translated password: %s", password); return 0; } ``` 以上就是您提出的问题的解答,希望能对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值