题解 | #字符串加密#
字符串加密
https://www.nowcoder.com/practice/e4af1fe682b54459b2a211df91a91cf3
#include<stdio.h>
int main(void)
{
char key[100];
char newkey[100]={0};
char origin[100]={0};
char secret[100]={0};
int len_key;
scanf("%[^\n]\n", &key);
scanf("%[^\n]", &origin);
len_key = strlen(key);
int i = 0;
int k = 0;
int temp = 0;
for (i = 0; i < len_key; i++)//输出新的key
{
for (int j = 0; j < i; j++)
{
if (key[i] == key[j])
{
temp++;
}
}
if (temp == 0)
{
newkey[k] = key[i];
k++;
}
else if (temp != 0)
{
}
temp = 0;
}
temp = 0;
k = 0;
int len_newkey = strlen(newkey);
for (char a = 'a'; a <= 'z'; a++)//生成新的字母表
{
for (i = 0; i < len_newkey; i++)
{
if (a == newkey[i])
{
temp++;
}
}
if (temp == 0)
{
newkey[len_newkey + k] = a;
k++;
}
else if(temp != 0)
{
}
temp = 0;
}
temp = 0;
k = 0;
int len_origin = strlen(origin);
for (i = 0; i < len_origin; i++)//字符加密
{
if (origin[i] >= 'a' && origin[i] <= 'z')
{
temp = origin[i] - 'a';
secret[i] = newkey[temp];
}
else if (origin[i] >= 'A' && origin[i] <= 'Z')
{
origin[i] +=32;
temp = origin[i] - 'a';
secret[i] = newkey[temp];
secret[i] -= 32;
}
else if (origin[i] == ' ')
{
secret[i] = ' ';
}
temp = 0;
}
printf("%s\n", secret);//输出加密字符串
return 0;
}
int main(void)
{
char key[100];
char newkey[100]={0};
char origin[100]={0};
char secret[100]={0};
int len_key;
scanf("%[^\n]\n", &key);
scanf("%[^\n]", &origin);
len_key = strlen(key);
int i = 0;
int k = 0;
int temp = 0;
for (i = 0; i < len_key; i++)//输出新的key
{
for (int j = 0; j < i; j++)
{
if (key[i] == key[j])
{
temp++;
}
}
if (temp == 0)
{
newkey[k] = key[i];
k++;
}
else if (temp != 0)
{
}
temp = 0;
}
temp = 0;
k = 0;
int len_newkey = strlen(newkey);
for (char a = 'a'; a <= 'z'; a++)//生成新的字母表
{
for (i = 0; i < len_newkey; i++)
{
if (a == newkey[i])
{
temp++;
}
}
if (temp == 0)
{
newkey[len_newkey + k] = a;
k++;
}
else if(temp != 0)
{
}
temp = 0;
}
temp = 0;
k = 0;
int len_origin = strlen(origin);
for (i = 0; i < len_origin; i++)//字符加密
{
if (origin[i] >= 'a' && origin[i] <= 'z')
{
temp = origin[i] - 'a';
secret[i] = newkey[temp];
}
else if (origin[i] >= 'A' && origin[i] <= 'Z')
{
origin[i] +=32;
temp = origin[i] - 'a';
secret[i] = newkey[temp];
secret[i] -= 32;
}
else if (origin[i] == ' ')
{
secret[i] = ' ';
}
temp = 0;
}
printf("%s\n", secret);//输出加密字符串
return 0;
}