Code has 2 functional issues.
"If there is space before the first word you get a blank line at the top." @Loki Astari @Loki Astari This is easily fixed in OP code with
int status = IN;
Should the last character be a non-white-space, no following
'\n'
is appended. The usual definition of a line a termination'\n'
and so such file do not meet the required "prints its input one word per line"
The following amends these issues.
#include <ctype.h>
#include <stdlib.h>
#include <stdbool.h>
void PrintOneWordPerLine(FILE *inf) {
bool previous_printable = false;
for (;;) {
int ch = fgetc(inf); // or getchar();
if (isspace(ch) || ch == EOF) {
if (previous_printable) {
previous_printable = false;
fputc('\n', stdout); // or putchar('\n');
}
if (ch == EOF) {
break; // or return
}
} else {
previous_printable = true;
fputc(ch, stdout);
}
}
}
I like to avoid negation, so instead of if (!previous_whitespace)
, used if (previous_printable)
Recommend using isspace()
Code has 2 functional issues.
"If there is space before the first word you get a blank line at the top." @Loki Astari This is easily fixed in OP code with
int status = IN;
Should the last character be a non-white-space, no following
'\n'
is appended. The usual definition of a line a termination'\n'
and so such file do not meet the required "prints its input one word per line"
The following amends these issues.
#include <ctype.h>
#include <stdlib.h>
#include <stdbool.h>
void PrintOneWordPerLine(FILE *inf) {
bool previous_printable = false;
for (;;) {
int ch = fgetc(inf); // or getchar();
if (isspace(ch) || ch == EOF) {
if (previous_printable) {
previous_printable = false;
fputc('\n', stdout); // or putchar('\n');
}
if (ch == EOF) {
break; // or return
}
} else {
previous_printable = true;
fputc(ch, stdout);
}
}
}
I like to avoid negation, so instead of if (!previous_whitespace)
, used if (previous_printable)
Recommend using isspace()
Code has 2 functional issues.
"If there is space before the first word you get a blank line at the top." @Loki Astari This is easily fixed in OP code with
int status = IN;
Should the last character be a non-white-space, no following
'\n'
is appended. The usual definition of a line a termination'\n'
and so such file do not meet the required "prints its input one word per line"
The following amends these issues.
#include <ctype.h>
#include <stdlib.h>
#include <stdbool.h>
void PrintOneWordPerLine(FILE *inf) {
bool previous_printable = false;
for (;;) {
int ch = fgetc(inf); // or getchar();
if (isspace(ch) || ch == EOF) {
if (previous_printable) {
previous_printable = false;
fputc('\n', stdout); // or putchar('\n');
}
if (ch == EOF) {
break; // or return
}
} else {
previous_printable = true;
fputc(ch, stdout);
}
}
}
I like to avoid negation, so instead of if (!previous_whitespace)
, used if (previous_printable)
Recommend using isspace()
Code has 2 functional issues.
"If there is space before the first word you get a blank line at the top." @Loki Astari This is easily fixed in OP code with
int status = IN;
Should the last character be a non-white-space, no following
'\n'
is appended. The usual definition of a line a termination'\n'
and so such file do not meet the required "prints its input one word per line"
The following amends these issues.
#include <ctype.h>
#include <stdlib.h>
#include <stdbool.h>
void PrintOneWordPerLine(FILE *inf) {
bool previous_printable = false;
for (;;) {
int ch = fgetc(inf); // or getchar();
if (isspace(ch) || ch == EOF) {
if (previous_printable) {
previous_printable = false;
fputc('\n', stdout); // or putchar('\n');
}
if (ch == EOF) {
break; // or return
}
} else {
previous_printable = true;
fputc(ch, stdout);
}
}
}
I like to avoid negation, so instead of if (!previous_whitespace)
, used if (previous_printable)
Recommend using isspace()