代码五分钟,debug一小时……

字符串加密

http://www.nowcoder.com/questionTerminal/e4af1fe682b54459b2a211df91a91cf3

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

void encrypt(char * key, char * data, char * out){
    int cnt = 0, i=0, j=0;
    char book[26] = {0};
    for(i=0; key[i] != '\0'; i++){
        if(key[i]>='a')
            key[i] -= 'a'-'A';

        for(j=0; j<cnt; j++){
            if(key[i]==book[j])
                break;
        }
        if(j==cnt){
            book[cnt] = key[i];
            cnt++;
        }
    }
    for(i='A'; i <= 'Z'; i++){
        for(j=0; j<cnt; j++){
            if(i==book[j])
                break;
        }
        if(j==cnt){
            book[cnt] = i;
            cnt++;
        }
    }
    // 加密
    for(i=0; data[i]!='\0'; i++){
        if(data[i]>='a'){
            data[i] -= 'a' - 'A';
            out[i] = book[data[i]-'A'] + 'a' - 'A';
        }else{
            out[i] = book[data[i]-'A'];
        }
    }
    out[i] = '\0';
}

int main(void){
    char key[1000];
    while(scanf("%s", &key) != EOF){
        char data[3000]={0}, out[3000]={0};
        scanf("%s", &data);
        encrypt(&key, &data, &out);
        printf("%s\n", out);
        bzero(key, 1000);
    }
    return 0;
}
全部评论
L47-48应该去掉&
点赞 回复 分享
发布于 2020-04-23 12:05

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务