wjj2643407465 @ 2020-12-29 16:49:39
#include<stdio.h>
#include<string.h>
int main(void)
{ int n,i;
char a[100],ch;
scanf("%d",&n);
ch = getchar(); //吃回车
gets(a);
for(i=0;i<strlen(a);i++){
if(a[i]+n<='z')a[i] = a[i] + n;
else a[i] = 'a' - 1 + n - ('z' - a[i]);
}
for(i=0;i<strlen(a);i++)
printf("%c",a[i]);
}
by wsyhb @ 2020-12-29 17:00:02
@wjj2643407465 修改后代码如下:
#include<stdio.h>
#include<string.h>
int main(void)
{ int n,i;
char a[100],ch;
scanf("%d",&n);
/*
ch = getchar(); //吃回车
gets(a);
*/
scanf("%s",a);//不要用 gets !!! 用 scanf !!!
for(i=0;i<strlen(a);i++){
if(a[i]+n<='z')a[i] = a[i] + n;
else a[i] = 'a' - 1 + n - ('z' - a[i]);
}
for(i=0;i<strlen(a);i++)
printf("%c",a[i]);
}
说了多少遍了,不要在洛谷用 gets()
,我已经看到第
Tips:字符串题建议用 Luogu IDE 调试
by wjj2643407465 @ 2020-12-30 11:03:07
假如输入的字符串中含有空格那咋办呢
by Sky3 @ 2021-02-08 22:20:07
因为n的值可能很大,在更新字符的时候就会发生错误的。要加一行
n = n % 26