0

My code reads a textfile or input from the terminal and prints out the message but with each word on a new line the code below works with the exception of double or more spaces.

Because I use the space as an indication that a whole word has ended. But I don't want a space to be printed as a blank separate line. Any help would be appreciated!!

A sample input would be:

Hello world this is great

The current output is:

Hello
world
this
is
great

The problem is if the input includes a sentence with two or more spaces it prints a blank line. I want the code to skip to the next word.

for instance

input: hello world  how are 
                  ^^(two spaces)
output:
hello
world
        < (I want this gap gone)
how
are

#include <ctype.h>
#include <stdio.h>
#include <unistd.h>

int main()
{
    int c;                          //next character



    while (1) {
        c = getchar();
        if (c == EOF){ break; }  // Exit the loop if we receive EOF ("end of file")
        if (ispunct(c)|| isdigit(c) || c== '\n') //ignores numbers and punnctuation
            continue;



        if (isspace(c)) {   // if there is a space end of word has been reach output the word
            printf("\n");
            continue;

        }
        usleep(200000); // delay between each word
        putchar(c);

    }
}
Tom Karzes
  • 22,815
  • 2
  • 22
  • 41
  • On a couple of unrelated notes: The `continue` statement will continue the loop immediately, the `fflush(stdout)` call after it will not be executed. Not that you really need it because output to `stdout` (which is where `printf` is writing) is by default line-buffered, so that `printf("\n")` call will include flushing the output buffer of `stdout`. – Some programmer dude Apr 09 '20 at 04:11
  • More related to your question, please [edit] it to include some sample input, and the output you expect from that input. Also please take some time to read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Apr 09 '20 at 04:13

1 Answers1

0

What you need is one more variable to remember what the previous character was, so you can distinguish a word ending and skip further separators until a new word is encountered.

In the fragment below I called that variable "pcwaswsp". The code is copied from yours, with two modifications: 1) the new variable and 2) I moved the check for words just after the reading.

int c;         // current character
int pcwassp;  // "previous character was space"

pcwassp = 0;  // previous char is not a space

while (1) {
  c = getchar();
  if (c == EOF) break;  // no more data
  // optional: if (pcwassp==2) pcwassp=isspace(c);

  // word separation
  if (isspace(c)) {
    if (!pcwassp)
      printf("\n");   // break line only on new word separator
    pcwassp = 1;      // ...but this char IS a space
    continue;
  } // else...

  pcwassp=0;   // ...else, this char is not a space

  //ignores numbers and punctuation
  ...
}

This code would print a new line if its input starts with a separator. To correct this, the variable pcwassp could be initialized to a special value (not 0 and not 1) having the meaning of "first time". The in the cycle, only the first time, this variable can be set to "previous char was a separator" (if the current char is a space) in order to prevent the first line output to be empty.