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);
}
}
continue
statement will continue the loop immediately, thefflush(stdout)
call after it will not be executed. Not that you really need it because output tostdout
(which is whereprintf
is writing) is by default line-buffered, so thatprintf("\n")
call will include flushing the output buffer ofstdout
. – Some programmer dude