Skip to main content
Code Review

Return to Answer

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

Code has 2 functional issues.

  1. "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;

  2. 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.

  1. "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;

  2. 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.

  1. "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;

  2. 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()

Source Link
chux
  • 36.1k
  • 2
  • 43
  • 96

Code has 2 functional issues.

  1. "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;

  2. 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()

lang-c

AltStyle によって変換されたページ (->オリジナル) /