【发布时间】:2021-12-11 18:41:52
【问题描述】:
char toFind[100];
char replace[100];
int pos = 0;
printf("Enter a text: ");
scanf("%[^\n]", str);
printf("Enter a search pattern: ");
scanf("%[^\n]", toFind);
printf("Enter a substitute: ");
scanf("%[^\n]", replace);
pos = strnfnd(0, toFind);
strins(pos, replace);
printf("Der Text ist: %s", str);
此代码示例让我读取 str 的值,但跳过了其他两个 scanf。我不知道为什么。 我该怎么做才能解决这个问题?
Ps:str是一个全局char数组
【问题讨论】:
-
快速修复:在对
scanf()的第二次和第三次调用中,在%[^n]之前放置一个空格。但是您最好使用fgets()代替。这里已经有很多类似的问题了。 -
请将
"%[^\n]"更改为" %[^\n]"并添加空格以过滤输入缓冲区中的前一个换行符。第一个是无害的。 -
什么是
str?strnfnd和strins是什么,如果后者与问题无关,为什么它们在您的代码中?请阅读:minimal reproducible example -
什么是字符串?
-
在我看来你应该考虑使用
fgets而不是scanf
标签: c scanf newline c-strings conversion-specifier