我想每行打印一个单词,但输出不打印每个单词的最后一个字符。任何人都可以帮助找到错误?
#include <stdio.h>
#define IN 1 /* Inside a text */
#define OUT 0 /* Outside a text */
main()
{
int c,nw,state;
state=OUT;
while((c=getchar())!=EOF)
{
if(c==' '||c=='\n'||c=='\t')
{
state=OUT;
printf("\n");
}
else if(state==OUT)
{
putchar(c); /* To print the first character */
state=IN;
++nw;
c=getchar();
putchar(c); /* To print the other characters of the word*/
}
}
}
使用上面的代码,不会打印每个单词的最后一个字符。
答案 0 :(得分:2)
我认为可以在不维持州的情况下解决。欢迎提出建议
#include <stdio.h>
int main(void) {
// your code goes here
int c;
while((c=getchar())!=EOF)
{
if(c==' ' || c=='\t' || c=='\b')
{
printf("\n");
while(c==' ' || c=='\t' || c=='\b')
c=getchar();
}
if(c!=EOF)
putchar(c);
}
return 0;
}
答案 1 :(得分:1)
要修复的示例
#include <stdio.h>
#define IN 1 /* Inside a text */
#define OUT 0 /* Outside a text */
int main(void){
int c, nw=0, state=OUT;
while((c=getchar())!=EOF){
if(c==' ' || c=='\n' || c=='\t'){//if(isspace(c)){
if(state == IN){
state = OUT;
putchar('\n');
}
} else {
if(state == OUT){
state = IN;
++nw;
}
putchar(c);
}
}
//printf("\n%d\n", nw);
return 0;
}
答案 2 :(得分:0)
我认为这是解决方案
#include <stdio.h>
main()
{
int c, inspace;
inspace = 0;
while ((c=getchar())!= EOF)
{
if (c == ' ' || c == '\t' || c == '\n')
{
if (inspace == 0)
{
putchar('\n');
inspace = 1;
}
}
else
{
putchar(c);
inspace = 0;
}
}
}
答案 3 :(得分:0)
for( ; (c = getchar()) != EOF; )
{
flag = 0;
if(c == '\n' || c == '\t' || c == ' ')
{ state = OUT;
}
else if(state == OUT){
flag = 1;
state = IN;
}
if(flag == 1 && count!=0)
putchar('\n');
if(state == IN)
putchar(c);
count++;
}
答案 4 :(得分:0)
你可以试试这个。
function addTimeout(fn, timeout) {
if (timeout === undefined) timeout = 1000; // default timeout
return function(...args) {
return new Promise(function(resolve, reject) {
const tid = setTimeout(reject, timeout,
new Error("promise timed out"));
fn(...args)
.then(function(...args) {
clearTimeout(tid);
resolve(...args);
})
.catch(function(...args) {
clearTimeout(tid);
reject(...args);
});
});
};
}
答案 5 :(得分:0)
#include<stdio.h>
int main(){
int c;
while((c=getchar())!=EOF)
if(c=='\t'|| c=='\n' || c==' ')
putchar('\n');
else
putchar(c);
return 0;
}
答案 6 :(得分:0)
每行打印输入一个单词的程序
OP的代码肯定与c=getchar(); putchar(c);
的以下内容有关,因为它打印c
是字母,空格,'\n'
或EOF
。< / p>
else if(state==OUT) {
putchar(c);
state=IN;
++nw;
c=getchar();
putchar(c); // Error to print without testing `c`
}
OP state
想法后只需要进行一些更改。
#include <stdio.h>
#include <ctype.h>
#define IN 1 /* Inside text */
#define OUT 0 /* Outside text */
int main(void) {
unsigned long long wc = 0; // no need for a narrow `int`, how about something wider?
int state = OUT;
int ch;
while ((ch = getchar()) != EOF) {
if (state == OUT) {
if (!isspace(ch)) {
wc++; // new word begun
state = IN;
putchar(ch);
}
} else /* state == IN */ {
if (isspace(ch)) { // add test
putchar('\n');
state = OUT;
} else {
putchar(ch);
}
}
}
if (state == IN) { // add test in case last word not followed by a white-space
putchar('\n');
}
printf("Word count:%llu\n", wc);
return 0;
}
答案 7 :(得分:0)
这是@BLUEPIXY的一个版本,仅在添加字符时才打印字符,然后在换行符上将单词分开,并在换行符的末尾填充换行符。
#include <stdio.h>
#define IN 1 /*Inside of a text*/
#define OUT 0 /*outside of a text*/
#define SPACE ' '
#define NEWLINE '\n'
#define TAB '\t'
int main()
{
int nw = 0, c, state = OUT;
while ((c = getchar()) != EOF)
{
if (c == SPACE || c == NEWLINE || c == TAB)
{
/* Pads the last character(a new line) with another new line */
if (c == NEWLINE)
{
putchar('\n');
}
if (state == IN)
{
state = OUT;
putchar('\n');
}
}
else
{
if (state == OUT)
{
state = IN;
++nw;
}
putchar(c);
}
}
printf("\nTotal word count: %d\n", nw);
return 0;
}
答案 8 :(得分:0)
其中一条评论说,如果没有#defines或标志,这是可以解决的。确实有可能。而且它使代码非常短。这是我的解决方案:
int main()
{
int c;
while ((c = getchar()) != EOF)
{
if (c != ' ')
{
putchar(c);
}
else
{
printf("\n");
}
}
}
答案 9 :(得分:0)
使用state
,但仅呼叫getchar
。
#include <stdio.h>
#define IN 1
#define OUT 0
int main() {
int c;
int state;
state = OUT;
while ((c = getchar()) != EOF) {
if (c == ' ' || c == '\t' || c == '\n') {
state = OUT;
} else if (state == OUT) {
state = IN;
putchar('\n');
putchar(c);
} else if (state == IN) {
putchar(c);
}
}
答案 10 :(得分:0)
您还可以:
#include <stdio.h>
#define IN 1
#define OUT 0
main()
{
int state;
int c;
state = OUT;
while ((c = getchar()) != EOF) {
if ((c != '\n') && (c != ' ') && (c != '\t')) {
putchar(c);
state = IN;
}
else if (state == IN) {
putchar('\n');
state = OUT;
}
}
}