题解 | #字符串加密#

字符串加密

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

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

//加密数组
char flag[26] = {0};
int flagSize = 0;

//检查target字符是否已存在数组中
int check(char target){
    for(int i = 0; i < flagSize; i++){
        if(flag[i]==target) return 1;
    }
    return 0;
}

int main(){
    char s[101] = {0};
    char ret[101] = {0};
    scanf("%s\n",s);
    scanf("%s\n",ret);
    int len = strlen(s);
    int len_2 = strlen(ret);

    //遍历,不存在的字符按顺序放入加密数组
    for(int i = 0; i < len; i++){
        if(!check(s[i])){
            flag[flagSize++] = s[i];
        }
    }

    //遍历a-z,不存在的字符按顺序放入加密数组      for(char i = 'a'; i <= 'z'; i++){
        if(!check(i)){
            flag[flagSize++] = i;
        }
    }

    //逐个字符对照加密数组打印
    for(int i = 0; i < len_2; i++){
        printf("%c",flag[ret[i]-'a']);
    }
    return 0;
}



全部评论

相关推荐

评论
7
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务