题解 | #字符串加密#
字符串加密
https://www.nowcoder.com/practice/e4af1fe682b54459b2a211df91a91cf3
#include <stdio.h> #include <string.h> int main(){ char key[28], text[102]; scanf("%s", key); getchar(); fgets(text, sizeof(text), stdin); text[strlen(text) - 1] = '\0'; int len_key = strlen(key); char buf[28]; for(int i = 0, k = 0; i < len_key; i++){ int flag = 0; for(int j = i - 1; j >= 0; j--){ if(key[i] == key[j]) flag++; } if(flag == 0) buf[k++] = key[i]; } int len_buf = strlen(buf); for(int i = 'a'; i <= 'z'; i++){ int flag = 0; for(int j = 0; j < len_buf; j++){ if(buf[j] == i) flag++; } if(flag == 0){ buf[len_buf++] = i; buf[len_buf] = '\0'; } } char dics[124][124]; for(int i = 'a', j = 0; i <= 'z'; i++){ *dics[i] = buf[j++]; } int len_text = strlen(text); char paswd[102]; for(int i = 0; i < len_text; i++){ if(text[i] >= 'a' && text[i]<= 'z'){ paswd[i] = *dics[text[i]]; paswd[i + 1] = '\0'; } else{ paswd[i] = text[i]; paswd[i + 1] = '\0'; } } printf("%s", paswd); return 0; }