|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<stdio.h>
#include<string.h>
void strmcpy(char*s,char*t,int m);
void strmcpy(char*s,char*t,int m)
{
strcpy(s, t + m - 1);
}
int main(void)
{
char s[20] = {0};
char t[20] = {0};
int m;
printf("从第几个字符开始复制:");
scanf("%d",&m);
printf("输入一串字符:");
gets(t);
strmcpy(s,t,m);
puts(s);
return 0;
}
为什么不能输入,换成scanf(“%s”, t);就可以
本帖最后由 Ranbo_ 于 2020-3-6 22:32 编辑
因为你的 读取到m之后,你需要键入一个空白符,像空格回车那些,scanf才知道,噢,我的m的值已经读到了,但是这导致你的标准输入流里会有遗留的字符,因此你的gets()函数无法正常读取数据。所以一般在当前语句从标准输入流里读取完之后要清空缓冲区,才能使后续的输入有效。
在scanf后面加一句清空缓冲区即可:
- #include<stdio.h>
- #include<string.h>
- void strmcpy(char*s,char*t,int m);
- void strmcpy(char*s,char*t,int m)
- {
- strcpy(s, t + m - 1);
- }
- int main(void)
- {
- char s[20] = {0};
- char t[20] = {0};
- int m;
- printf("从第几个字符开始复制:");
- scanf("%d",&m);
- fflush(stdin);
- printf("输入一串字符:");
- gets(t);
- strmcpy(s,t,m);
- puts(s);
- return 0;
- }
复制代码
|
|