题解 | #字符串加密#
字符串加密
http://www.nowcoder.com/practice/e4af1fe682b54459b2a211df91a91cf3
#include "stdio.h"
#include "string.h"
int main()
{
char key[101];
char buf[101];
while(scanf("%s",key)!=EOF)
{
memset(buf,0,100);
scanf("%s",buf);
int len_key = strlen(key);
int len_buf = strlen(buf);
char letter[26],count=0;
for(int i=0;i<len_key;i++) // 排除密码key字符串重复项
{
char flag=0;
char temp;
for(int j=0;j<count;j++)
{
if(key[i]>='a' && key[i]<='z')
temp = key[i]-'a';
else if (key[i]>='A' && key[i]<='Z')
temp = key[i]-'A';
if(temp==letter[j])
{
flag=1;
break;
}
}
if(flag==0)
{
if(key[i]>='a' && key[i]<='z')
letter[count++] = key[i]-'a';
else if(key[i]>='A' && key[i]<='Z')
letter[count++] = key[i]-'A';
}
}
for(int i=0;i<26;i++) // 创建密码表
{
char flag=0;
for(int j=0;j<count;j++)
{
if(letter[j]==i)
{
flag=1;
break;
}
}
if(flag==0)
{
letter[count++] = i;
}
}
for(int i=0;i<len_buf;i++) // 加密字符串
{
if(buf[i]>='a' && buf[i]<='z')
{
buf[i]= letter[buf[i]-'a']+'a';
}
else if(buf[i]>='A' && buf[i]<='Z')
{
buf[i]= letter[buf[i]-'A']+'A';
}
else
{
continue;
}
}
printf("%s\n",buf);
}
return 0;
}
哈皮加密思路