//c和指针第9章 第12题 加密数据
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int prepare_key(char *key,char *str)
{
int i = 0,j = 0;
char flag = 'a';
if(strpbrk(key," \n\t") != NULL)
return 0;
while(*key)
{
*key = tolower(*key);
if(strchr(str,*key) == NULL)
{
str[j] = *key;
str[j + 1]= '\0';
j++;
}
key++;
}
while(flag <= 'z')
{
if(strchr(str,flag) == NULL)
{
str[j] = flag;
str[j + 1] = '\0';
j++;
}
flag++;
}
puts(str);
return 1;
}
void encrypt(char *data,const char *key)
{
int i;
char *p = data;
while(*p)
{
if (islower(*p))
{
i = *p - 'a';
*p = *(key + i);
}
else if(isupper(*p))
{
i = *p - 'A';
*p = toupper(*(key + i));
}
p++;
}
puts(data);
}
void decrypt(char *data,char const *key)
{
int i;
char *p = data;
while(*p)
{
if (islower(*p))
{
i = strchr(key,*p) - key;
*p = 'a' + i;
}
else if(isupper(*p))
{
i = strchr(key,tolower(*p)) - key;
*p = 'A' + i;
}
p++;
}
puts(data);
}
int main(void)
{
char s1[] = "TRAILBLAZERS";
char s2[] = "aTTAck at DAwn";
char s3[27] = "";
prepare_key(s1,s3);
encrypt(s2,s3);
decrypt(s2,s3);
system("pause");
return 0;
}
《c 和指针》第9章第12题——加密数据
最新推荐文章于 2022-09-21 00:45:41 发布