fgets()和scanf()不能同时正常工作。遇到缓冲区问题
我的作业:- 编写一个程序来替换给定字符的出现(例如 c) 在一个主字符串(如PS)和另一个字符串(如s)中 输入:第一行包含下一行的主字符串(PS) 包含字符(c)下一行包含字符串(s) 输出:打印字符串PS,每次出现c都被s替换 测试用例1:- 输入:- abcxy b mf 预期产出:- amfcxy 测试用例2:- 输入:- Al@bal#20猫头鹰 l LL 预期产出:- ALL@baLL#20猫头鹰 我的代码如下:-fgets()和scanf()不能同时正常工作。遇到缓冲区问题,c,char,scanf,buffer,fgets,C,Char,Scanf,Buffer,Fgets,我的作业:- 编写一个程序来替换给定字符的出现(例如 c) 在一个主字符串(如PS)和另一个字符串(如s)中 输入:第一行包含下一行的主字符串(PS) 包含字符(c)下一行包含字符串(s) 输出:打印字符串PS,每次出现c都被s替换 测试用例1:- 输入:- abcxy b mf 预期产出:- amfcxy 测试用例2:- 输入:- Al@bal#20猫头鹰 l LL 预期产出:- ALL@baLL#20猫头鹰 我的代码如下:- 现在我的代码运行良好,但输出仍然不正确。有时它无法从字符串S复制最
现在我的代码运行良好,但输出仍然不正确。有时它无法从
字符串S
复制最后一个字符,或给我胡言乱语的输出。代码的问题是:-
i = i - 1; //i now holds the value of the size of the string PS (excluding '\0')
j = j - 1; //j now holds the value of the size of the string S (excluding '\0')
i
和j
的值是字符串PS
和字符串S
大小的真实值;不是i=i-1
和j=j-1
从这次作业中吸取的教训:-
scanf()
不以任何方式处理'\n'
。它将留在
缓冲区fgets
,然后从相应的数组/指针中删除'\n'
字符和字符串时,要格外小心C
缓冲区
#include <stdio.h>
#include <stdlib.h>
int main()
{
char PS[101];
char c;
char S[11];
fgets(PS, 101, stdin); //PS value input.
scanf("%c", &c);
if(c == '\n' || c == '\0')
{
scanf("%c", &c); //Clearing the buffer. I want the real value of 'c' from STDIN not '\n'
}
char x;
x = getchar(); //New addition. It eats the '\n' after scanf().
fgets(S, 11, stdin); //S value input.
int i = 0;
while(PS[i] != '\0') //Removing the '\n' from PS
{
if(PS[i] == '\n')
{
PS[i] = '\0';
break;
}
i++;
}
i = i; //i now holds the value of the size of the string PS (excluding '\0')
int j = 0;
while(S[j] != '\0')
{
if(S[j] == '\n')
{
S[j] = '\0';
break;
}
j++;
}
j = j; //j now holds the value of the size of the string S (excluding '\0')
int k = 0; //work as an initializer
int move = 0; //work as an initializer.
while(PS[k] != '\0') //This loops checks the whole array for the same character mentioned in char 'c'
{
if(PS[k] == c)
{
for(move = i; move > k; move --) //This loop advances the all the characters in PS by '(j - 1)' steps to make space for string S characters.
{
PS[move + (j - 1)] = PS[move];
}
for(move = 0; move < j; move++) //This loop adds all the characters of string S into string PS at the relevant place.
{
PS[k + move] = S[move];
}
i = i + (j - 1); // 'i' now holds the new value of size of string PS after adding all the characters of string S.
}
k++;
}
puts(PS);
return 0;
}
#包括
#包括
int main()
{
char-PS[101];
字符c;
chars[11];
fgets(PS,101,stdin);//PS值输入。
scanf(“%c”、&c);
如果(c=='\n'| | c=='\0')
{
scanf(“%c”,&c);//正在清除缓冲区。我希望STDIN中的'c'的实际值不是'\n'
}
字符x;
x=getchar();//新添加的内容。它将在scanf()之后吃“\n”。
fgets(S,11,stdin);//S值输入。
int i=0;
while(PS[i]!='\0')//从PS中删除'\n'
{
如果(PS[i]='\n')
{
PS[i]='\0';
打破
}
i++;
}
i=i;//我现在保存字符串PS的大小值(不包括“\0”)
int j=0;
而(S[j]!='\0')
{
如果(S[j]='\n')
{
S[j]='\0';
打破
}
j++;
}
j=j;//j现在保存字符串S的大小值(不包括“\0”)
int k=0;//用作初始值设定项
int move=0;//用作初始值设定项。
while(PS[k]!='\0')//此循环检查整个数组中是否存在字符“c”中提到的相同字符
{
if(PS[k]==c)
{
for(move=i;move>k;move--)//此循环将PS中的所有字符按“(j-1)”步前进,以便为字符串S字符留出空间。
{
PS[move+(j-1)]=PS[move];
}
for(move=0;move
警告:-
请看。简单的答案是不要把它们混在一起。使用
fgets()
获取所有输入。顺便说一句,我会使用比你更大的字符串。首先,您可以使用两倍于您想象所需的缓冲区大小<代码>字符S[11]看起来好像您有非常具体的想法,但不要忘记换行符、字符串终止符、随机空格等。要“刷新”或清除stdin的输入缓冲区,请不要使用scanf
。首先,你用错了,其次,对于这么简单的东西来说,它太强大了。首先,您需要一个循环,然后在循环中使用,例如,直到您阅读了换行符或EOF
。但是请注意,getchar
返回一个int
,这对于EOF
检查很重要。@某个程序员我知道我的代码很糟糕,但是你能不能为我的“编辑”给出一个答案,以及我应该使用什么方法来保持缓冲区干净?如果你使用的是fgets()
对于所有输入,您不需要“清除缓冲区”。输入一个字符串,然后输入另一个字符串。换行符被读入字符串,并且没有任何'\0'
字符进入。因此,您根本不需要scanf()
。请参阅,尽管搜索字符显然只是该特定字符串的第一个字符。“代码运行正常,但输出仍然不正确。”-->与其只描述输出,不如发布准确的输出。
i = i - 1; //i now holds the value of the size of the string PS (excluding '\0')
j = j - 1; //j now holds the value of the size of the string S (excluding '\0')
#include <stdio.h>
#include <stdlib.h>
int main()
{
char PS[101];
char c;
char S[11];
fgets(PS, 101, stdin); //PS value input.
scanf("%c", &c);
if(c == '\n' || c == '\0')
{
scanf("%c", &c); //Clearing the buffer. I want the real value of 'c' from STDIN not '\n'
}
char x;
x = getchar(); //New addition. It eats the '\n' after scanf().
fgets(S, 11, stdin); //S value input.
int i = 0;
while(PS[i] != '\0') //Removing the '\n' from PS
{
if(PS[i] == '\n')
{
PS[i] = '\0';
break;
}
i++;
}
i = i; //i now holds the value of the size of the string PS (excluding '\0')
int j = 0;
while(S[j] != '\0')
{
if(S[j] == '\n')
{
S[j] = '\0';
break;
}
j++;
}
j = j; //j now holds the value of the size of the string S (excluding '\0')
int k = 0; //work as an initializer
int move = 0; //work as an initializer.
while(PS[k] != '\0') //This loops checks the whole array for the same character mentioned in char 'c'
{
if(PS[k] == c)
{
for(move = i; move > k; move --) //This loop advances the all the characters in PS by '(j - 1)' steps to make space for string S characters.
{
PS[move + (j - 1)] = PS[move];
}
for(move = 0; move < j; move++) //This loop adds all the characters of string S into string PS at the relevant place.
{
PS[k + move] = S[move];
}
i = i + (j - 1); // 'i' now holds the new value of size of string PS after adding all the characters of string S.
}
k++;
}
puts(PS);
return 0;
}