C:换行检查后在上一行打印
我是C语言的初学者,我想知道是否有一种方法可以在不换行的情况下,将我的字符数打印在与putchar()函数相同的行上C:换行检查后在上一行打印,c,C,我是C语言的初学者,我想知道是否有一种方法可以在不换行的情况下,将我的字符数打印在与putchar()函数相同的行上 #include <stdio.h> int main(void) { int c, i = 0; while ((c = getchar()) != EOF) { i++; if(putchar(c) == '\n'){ printf(":%d\n", i - 1);
#include <stdio.h>
int main(void) {
int c, i = 0;
while ((c = getchar()) != EOF) {
i++;
if(putchar(c) == '\n'){
printf(":%d\n", i - 1);
i = 0;
}
}
return 0;
}
输出
This is the first line.
This is the first line.
:23
有没有一种方法可以让输出看起来像这样
This is the first line.:23
很简单:因为
putchar
在调用它的时候输出,所以现在还不输出:
提示:在调用
putchar
之前检查您的字符。欢迎使用堆栈溢出。请注意,在这里说“谢谢”的首选方式是投票选出好的问题和有用的答案(一旦你有足够的声誉这么做),并接受对你提出的任何问题最有用的答案(这也会给你的声誉带来一点提升)。请看这页,也请看,这是固定的。非常感谢。
if (c == '\n') {
// omit newline here so that no empty lines are printed
printf(":%d", i - 1);
i = 0;
}
putchar(c);