其他分享
首页 > 其他分享> > 字符串(搬運)

字符串(搬運)

作者:互联网

char vowels[] = "aeiouAEIOU";

bool isVowel(char c) {             // (1)
    int i;
    for(i = 0; vowels[i]; ++i) {
        if(vowels[i] == c) {
            return true;
        }
    }    
    return false;
}

void swap(char *a, char *b) {       // (2)
    char tmp = *a;
    *a = *b;
    *b = tmp;
}

char * reverseVowels(char * s){
    int i = 0, j = strlen(s)-1;     // (3)
    while(i < j) {
        while(s[i] && !isVowel(s[i]))
            ++i;                    // (4)
        while(j >= 0 && !isVowel(s[j]))
            --j;                    // (5)
        if(i >= j) break;           // (6)
        swap( &s[i], &s[j] );       // (7)
        ++i, --j;                   // (8)
    }
    return s;

}

char *filter(char *s) {                          // (1)
    int i;
    char *ret = (char *) malloc( sizeof(char) * (strlen(s)+1) );
    int retSize = 0;
    for(i = 0; s[i]; ++i) {
        if(s[i] >= 'a' && s[i] <= 'z') {
            ret[retSize++] = s[i];               // (2)
        }else if(s[i] >= 'A' && s[i] <= 'Z') {
            ret[retSize++] = s[i] - 'A' + 'a';   // (3)
        }else if(s[i] >= '0' && s[i] <= '9') {
            ret[retSize++] = s[i];               // (4)
        }
    }
    ret[retSize] = '\0';    
    return ret;
}

bool judgePalindrome(char *s) {                  // (5)
    int i;
    int len = strlen(s);
    for(i = 0; i < len/2; ++i) {
        if(s[i] != s[len-1-i]) {
            return false;
        }
    }
    return true;
}

bool isPalindrome(char * s){
    int i;
    char *t = filter(s);
    bool ret = judgePalindrome(t);
    free(t);
    return ret;
}

标签:return,int,vowels,搬運,char,++,&&,字符串
来源: https://www.cnblogs.com/sixiaoxiaoya/p/16339434.html