请编程序将:输入单词译成密码,密码规律是:用原来的字母后面的第4个字母代替原来的字母。

求解,用c语言
例如,字母'A'后面第4个字母是"E",用"E"代替"A","Z"用"D"代替。例如,输入"China"应译为"Glmre"。
请编一程序,将输入单词译为密码后输出。
(回车结束单词输入;单词最长20,之后截断;输入单词长度为0或者输入不为字母,输出error)。

C语言程序:

#include <stdio.h>
#include <string.h>

#define MAX 100

int isValidate(char str[]);
int isLetter(char ch);
int isLow(char ch);
void encrypt(char source[], char dest[]);

void main()
{
char source[MAX];
char dest[MAX];

printf("input a string : ");
gets(source);


if(isValidate(source) == 0)
{
printf("error\n");
return;
}

if(strlen(source) > 20)
{
source[20] = '\0';
}

encrypt(source, dest);

printf("encrypted : %s\n", dest);
}

/* åˆ¤æ–­å­—符串str是否合法 */
int isValidate(char str[])
{
int i, len;

len = strlen(str);

if(len <= 0)
{
return 0;
}

for(i=0; i<len; i++)
{
if(isLetter(str[i]) == 0)
{
return 0;
}
}

return 1;
}

/* åˆ¤æ–­å­—符ch是否是字母 */
int isLetter(char ch)
{
if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
{
return 1;
}
else
{
return 0;
}
}

/* åˆ¤æ–­å­—符ch是否是小写字母 */
int isLow(char ch)
{
if(ch >= 'a' && ch <= 'z')
{
return 1;
}
else
{
return 0;
}
}

/* åŠ å¯†å­—符串 */
void encrypt(char source[], char dest[])
{
int len = strlen(source);

for(int i=0; i<len; i++)
{
if(isLow(source[i]) == 1)
{
dest[i] = (source[i] - 'a' + 4) % 26 + 'a';
}
else
{
dest[i] = (source[i] - 'A' + 4) % 26 + 'A';
}
}

dest[i] = '\0';
}


运行测试:

input a string : China
encrypted : Glmre
温馨提示:答案为网友推荐,仅供参考