#include
#define M 100
int main()
{
char a[M];
int i;
while(scanf("%s",&a)!=EOF){//Ctrl+z结束循环
for(i=0;a[i]!='\0';++i){
a[i]+=3;
if(a[i]>'z'||(a[i]>'Z'&&a[i]<'a')){//判断是否超出字母范围内
a[i]-=26;
}
}
printf("%s\n",a);//输出结果
}
return 0;
}
测试结果
#include
int main()
{char c;
while((c=getchar())!='\n')
putchar((c+3)%32>26?c-23:c+3);
return 0;
}
取余数运算的应用,代码如下:
#include
#define L 100
int main()
{
char str[L+1], ch;
int i;
scanf("%s", &str);
for(i = 0; i < L+1; ++i){
ch = str[i];
if(ch == 0) break;
if((ch>='a')&&(ch<='z')){
str[i] = ((ch-'a') + 3) % 26 + 'a';
} else if((ch>='A')&&(ch<='Z')){
str[i] = ((ch-'A') + 3) % 26 + 'A';
} else {
printf("error!");
return 1;
}
}
if(i == L+1){
printf("error!");
return 1;
}
printf("%s", str);
return 0;
}