题解 | #字符串加解密#

字符串加解密

https://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a

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

void lock(char *str, int len){
    for(int i = 0; i < len; i++){
        if((str[i] >= 'a' && str[i] <= 'z')){
            if(str[i] == 'z')
                str[i] = 'A';
            else
                str[i] = str[i] + 1 - 32;
        }  
        else if((str[i] >= 'A' && str[i] <= 'Z')){
            if(str[i] == 'Z')
                str[i] = 'a';
            else
                str[i] = str[i] + 1 +32;
        }
        else if((str[i] >= '0' && str[i] <= '9')){
            if(str[i] == '9')
                str[i] = '0';
            else 
                str[i] = str[i] + 1;
        }

    }

}

void unlock(char *str, int len){
    for(int i = 0; i < len; i++){
        if((str[i] >= 'a' && str[i] <= 'z')){
            if(str[i] == 'a')
                str[i] = 'Z';
            else
                str[i] = str[i] - 1 - 32;
        }  
        else if((str[i] >= 'A' && str[i] <= 'Z')){
            if(str[i] == 'A')
                str[i] = 'z';
            else
                str[i] = str[i] - 1 +32;
        }
        else if((str[i] >= '0' && str[i] <= '9')){
            if(str[i] == '0')
                str[i] = '9';
            else 
                str[i] = str[i] - 1;
        }
        
    }

}

int main(){

    char mi[1002], fmi[1002];
    memset(mi, '\0', sizeof(mi));
    memset(fmi, '\0', sizeof(mi));
    scanf("%s", mi);
    int len = strlen(mi);


    for(int i = 0; i < len; i++){
        if((mi[i] >= 'a' && mi[i] <= 'z') || (mi[i] >= 'A' && mi[i] <= 'Z') || (mi[i] >= '0' && mi[i] <= '9')){
            continue;
        }
        else
            return 0;
    }

    lock(mi, len);
    printf("%s\n", mi);

    scanf("%s", fmi);
    int len1 = strlen(fmi);

    for(int i = 0; i < len1; i++){
        if((fmi[i] >= 'a' && fmi[i] <= 'z') || (fmi[i] >= 'A' && fmi[i] <= 'Z') || (fmi[i] >= '0' && fmi[i] <= '9')){
            continue;
        }
        else
            return 0;
    } 

    unlock(fmi, len1);
    printf("%s", fmi);

    return 0;
}

全部评论

相关推荐

M_bao:换个排版吧哥们,看着费劲
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务