检测 space 是否被按下 3 次?
Detect if space is pressed 3 times?
在我的程序中,我需要检测 space 栏是否被按下 3 次,然后将其替换为 \n。
我正在使用 getchar 获取我的输入并检测一个 space 没问题,但如果我输入 3 space 来检查它不起作用。
非常感谢任何帮助
到目前为止,这是我的代码 如果我只检查一个 space 条,它工作得很好,但如果我输入其中的 3 个,它将检测不到它
if (c == ' ')
{
putchar('\n');
}
你可以数出连续空格的个数。类似于:
int c;
int spaces = 0;
while((c = getchar()) != EOF)
{
if (c == ' ')
{
++spaces;
if (spaces == 3)
{
putchar('\n');
spaces = 0;
}
}
else
{
spaces = 0;
}
}
在我的程序中,我需要检测 space 栏是否被按下 3 次,然后将其替换为 \n。
我正在使用 getchar 获取我的输入并检测一个 space 没问题,但如果我输入 3 space 来检查它不起作用。 非常感谢任何帮助
到目前为止,这是我的代码 如果我只检查一个 space 条,它工作得很好,但如果我输入其中的 3 个,它将检测不到它
if (c == ' ')
{
putchar('\n');
}
你可以数出连续空格的个数。类似于:
int c;
int spaces = 0;
while((c = getchar()) != EOF)
{
if (c == ' ')
{
++spaces;
if (spaces == 3)
{
putchar('\n');
spaces = 0;
}
}
else
{
spaces = 0;
}
}