题解 | #字符串加密#
字符串加密
http://www.nowcoder.com/practice/e4af1fe682b54459b2a211df91a91cf3
#include <stdio.h>
/*
函数功能:查询pass数组中是否存在target字符。
存在返回1,不存在返回0
*/
static int check_str(char *pass, int lenpass, char target)
{
for(int i = 0; i < lenpass; i++)
{
if(pass[i] == target)
{
return 1;
}
}
return 0;
}
int main()
{
char pass[32] = {0};
int index = 0;
char str[100] = {0};
char str_ret[100] = {0};
gets(str);
gets(str_ret);
int len = strlen(str);
int len_ret = strlen(str_ret);
for(int i = 0; i < len; i++)
{
if(check_str(pass, index, str[i]) == 0)
{
pass[index++] = str[i];
}
}
for(int i = 'a'; i <= 'z'; i++)
{
if(check_str(pass, index, i) == 0)
{
pass[index++] = i;
}
}
pass[26] = '\0'; //加密后的数据,结尾加上'\0'标志
//printf("pass = [%d : %s]\n", index, pass);
for(int i = 0; i < len_ret; i++)
{
char s = pass[str_ret[i]-'a'];
printf("%c", s);
}
printf("\n");
return 0;
}