C, problem with news and tabs next to each other

Here is my original code:

#include <stdio.h>

#define IN  1   // inside a word
#define OUT 0   // outside a word

// program to print input one word per line

int main(void)
{
  int c, state;

  state = OUT;
  while ((c = getchar()) != EOF) {
    if (c == ' ' || c == '\n' || c == '\t') {
      state = OUT;
      printf("\n");
    }
    else if (state == OUT) {
      state = IN;
    }
    if (state == IN) {
      putchar(c);
    }
  }
  return 0;
}

But the problem was that there were two spaces (spaces) or several tabs for which a new line was printed. So I used the variable (last) to track where I was:

#include <stdio.h>

#define IN  1   // inside a word
#define OUT 0   // outside a word

// program to print input one word per line, corrected bug if there was
// more than one space between words to only print one \n

int main(void)
{
  int c, last, state;

  last = EOF;
  state = OUT;
  while ((c = getchar()) != EOF) {
    if (c == ' ' || c == '\n' || c == '\t') {
      if (last != c) {
        state = OUT;
        printf("\n");
      }
    }
    else if (state == OUT) {
      state = IN;
    }
    if (state == IN) {
      putchar(c);
    }
    last = c;
  }
  return 0;
}

This resolved this, except that now that there is a [blank] [tab] between the pages, a new line is printed for both.

Can anyone help?

+3
source share
4 answers

Your problem with the source code is that you will output your new line for each space character. You want to do this only when moving from word to non-word:

Edit:

if (c == ' ' || c == '\n' || c == '\t') {
    state = OUT;
    printf("\n");
}

at

if (c == ' ' || c == '\n' || c == '\t') {
    if (state == IN) printf("\n");
    state = OUT;
}

, , :

enum eState {IN, OUT};
:
enum eState state = OUT;

:

#include <stdio.h>

#define FALSE (1==0)
#define TRUE  (1==1)
// Or: enum eBoolean {FALSE = 0, TRUE = 1};

int main (void) {
    int ch;
    int inWord = FALSE;     // Or: enum eBoolean inWord = FALSE;

    // Process every character.
    while ((ch = getchar()) != EOF) {
        // Check for whitespace.
        if (ch == ' ' || ch == '\n' || ch == '\t') {
            // Check if transitioning nonwhite to white.
            if (inWord) {
                printf("\n");
            }

            // Mark white no matter what.
            inWord = FALSE;
        } else {
            // Mark non whitespace.
            inWord = TRUE;
        }

        // If not whitespace, output character.
        if (inWord) {
            putchar(ch);
        }
    }
    return 0;
}
+6

paxdiablo, (FSA). OUT IN .

. , , FSA. . , , - . , . .

, . , , .

#include <stdio.h>

#define IN  1   // inside a word
#define OUT 0   // outside a word

// program to print input one word per line

int main(void)
{
  int c, state;

  state = OUT;
  while ((c = getchar()) != EOF) {
    switch (state){
    case OUT:
        switch (c){
        case ' ': case '\n': case '\t':
        break;
        default:
            putchar(c);
            state = IN;
        }
    break;
    case IN:
        switch (c){
        case ' ': case '\n': case '\t':
            putchar('\n');
            state = OUT;
        break;
        default:
            putchar(c);
        }
    break;
    }        
  }
  return 0;
}
+2

,

if (last != c) {

. last , . . X.

, , , last , X. , last!=current. , . . , .

 if (last != ' ' && last != '\n' && last != '\t' ) {

:

#include <stdio.h>

#define IN  1                   // inside a word
#define OUT 0                   // outside a word

// program to print input one word per line, corrected bug if there was
// more than one space between words to only print one \n

int main(void)
{
    int c, last, state;

    last = 0;  // We need it to make sure that a newline is not printed in case first
               // char is space, tab or new line.
    state = OUT;
    while ((c = getchar()) != EOF) {
        if (c == ' ' || c == '\n' || c == '\t') {
            // if (last != c)
            if (last != ' ' && last != '\n' && last != '\t' && last != 0 )
            {
                state = OUT;
                printf("\n");
            }
        } else if (state == OUT) {
            state = IN;
        }
        if (state == IN) {
            putchar(c);
        }
        last = c;
    }
    return 0;
}

Edit
​​, paxdiablo.

+1
#include<stdio.h>

#define OFF 0
#define ON 1
main()
{
  int c,state=ON;
  while((c=getchar())!=EOF)
    {
      if(c=='\n'||c==' '||c=='\t')
       {
         if(state==OFF)putchar('\n');
         state=ON;
       }
      else if(state==ON)
       {
         putchar(c);
         state=OFF;
       }
      else if(state==OFF)
       {
         putchar(c);
       }
    }
}

, :

Where, STE=Space, tab or enter.

<STE><WORD>---->TYPE<WORD>
<STE><STE>----->DO NOTHING
<WORD><SPACE>-->TYPE<WORD><ENTER/NEWLINE>
<WORD><WORD>--->TYPE<WORD>

You can replace and enable and disable as shown above.

0
source

Source: https://habr.com/ru/post/1770448/


All Articles