代码五分钟,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; }