Skip to main content
Code Review

Return to Answer

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

syb0rg suggested already syb0rg suggested already to initialize all variables, and actually the uninitialized variable nb causes a potential error in your program: An initial space character might not be preserved.

If you initialize the variable to zero

int nb = 0;

the check if (nb >= 1) after finding a space character is not necessary anymore, and that part can be simplified to

if (c == ' ' || c == '\n' || c == '\t')
 nb = nb + 1;
else
 nb = 0;

The output of the program still contains all kind of whitespace (space, newline or tab character), because the first whitespace character in a sequence is preserved. This means that

a<space><NL>b is transformed to a<space>b
a<NL><space>b is transformed to a<NL>b

It might be a good idea to replace all whitespace sequences by a space character:

if (c == ' ' || c == '\n' || c == '\t') {
 if (nb == 0) {
 putchar(' ');
 }
 nb = nb + 1;
} else {
 nb = 0;
 putchar(c);
}

You can also use isspace() to check for whitespace characters (but note that this function is locale-dependent).

syb0rg suggested already to initialize all variables, and actually the uninitialized variable nb causes a potential error in your program: An initial space character might not be preserved.

If you initialize the variable to zero

int nb = 0;

the check if (nb >= 1) after finding a space character is not necessary anymore, and that part can be simplified to

if (c == ' ' || c == '\n' || c == '\t')
 nb = nb + 1;
else
 nb = 0;

The output of the program still contains all kind of whitespace (space, newline or tab character), because the first whitespace character in a sequence is preserved. This means that

a<space><NL>b is transformed to a<space>b
a<NL><space>b is transformed to a<NL>b

It might be a good idea to replace all whitespace sequences by a space character:

if (c == ' ' || c == '\n' || c == '\t') {
 if (nb == 0) {
 putchar(' ');
 }
 nb = nb + 1;
} else {
 nb = 0;
 putchar(c);
}

You can also use isspace() to check for whitespace characters (but note that this function is locale-dependent).

syb0rg suggested already to initialize all variables, and actually the uninitialized variable nb causes a potential error in your program: An initial space character might not be preserved.

If you initialize the variable to zero

int nb = 0;

the check if (nb >= 1) after finding a space character is not necessary anymore, and that part can be simplified to

if (c == ' ' || c == '\n' || c == '\t')
 nb = nb + 1;
else
 nb = 0;

The output of the program still contains all kind of whitespace (space, newline or tab character), because the first whitespace character in a sequence is preserved. This means that

a<space><NL>b is transformed to a<space>b
a<NL><space>b is transformed to a<NL>b

It might be a good idea to replace all whitespace sequences by a space character:

if (c == ' ' || c == '\n' || c == '\t') {
 if (nb == 0) {
 putchar(' ');
 }
 nb = nb + 1;
} else {
 nb = 0;
 putchar(c);
}

You can also use isspace() to check for whitespace characters (but note that this function is locale-dependent).

added 143 characters in body
Source Link
Martin R
  • 24.2k
  • 2
  • 38
  • 96

syb0rg suggested already to initialize all variables, and actually the uninitialized variable nb causes a potential error in your program: An initial space character might not be preserved. In particular with

If you initialize the initializationvariable to zero

int nb = 0;

the check if (nb >= 1) after finding a space character is not necessary anymore, and that part can be simplified to

if (c == ' ' || c == '\n' || c == '\t')
 nb = nb + 1;
else
 nb = 0;

The output of the program still contains all kind of whitespace (space, newline or tab character), because the first whitespace character in a sequence is preserved. This means that

a<space><NL>b is transformed to a<space>b
a<NL><space>b is transformed to a<NL>b

It might be a good idea to replace all whitespace sequences by a space character:

if (c == ' ' || c == '\n' || c == '\t') {
 if (nb == 0) {
 putchar(' ');
 }
 nb = nb + 1;
} else {
 nb = 0;
 putchar(c);
}

You can also use isspace() to check for whitespace characters (but note that this function is locale-dependent).

syb0rg suggested already to initialize all variables. In particular with the initialization

int nb = 0;

the check if (nb >= 1) after finding a space character is not necessary anymore, and that part can be simplified to

if (c == ' ' || c == '\n' || c == '\t')
 nb = nb + 1;
else
 nb = 0;

The output of the program still contains all kind of whitespace (space, newline or tab character), because the first whitespace character in a sequence is preserved. This means that

a<space><NL>b is transformed to a<space>b
a<NL><space>b is transformed to a<NL>b

It might be a good idea to replace all whitespace sequences by a space character:

if (c == ' ' || c == '\n' || c == '\t') {
 if (nb == 0) {
 putchar(' ');
 }
 nb = nb + 1;
} else {
 nb = 0;
 putchar(c);
}

You can also use isspace() to check for whitespace characters (but note that this function is locale-dependent).

syb0rg suggested already to initialize all variables, and actually the uninitialized variable nb causes a potential error in your program: An initial space character might not be preserved.

If you initialize the variable to zero

int nb = 0;

the check if (nb >= 1) after finding a space character is not necessary anymore, and that part can be simplified to

if (c == ' ' || c == '\n' || c == '\t')
 nb = nb + 1;
else
 nb = 0;

The output of the program still contains all kind of whitespace (space, newline or tab character), because the first whitespace character in a sequence is preserved. This means that

a<space><NL>b is transformed to a<space>b
a<NL><space>b is transformed to a<NL>b

It might be a good idea to replace all whitespace sequences by a space character:

if (c == ' ' || c == '\n' || c == '\t') {
 if (nb == 0) {
 putchar(' ');
 }
 nb = nb + 1;
} else {
 nb = 0;
 putchar(c);
}

You can also use isspace() to check for whitespace characters (but note that this function is locale-dependent).

added 760 characters in body
Source Link
Martin R
  • 24.2k
  • 2
  • 38
  • 96

syb0rg'ssyb0rg suggested already to initialize all variables. In particular with the initialization

int nb = 0;

the check if (nb >= 1) after finding a space character is not necessary anymore, and that part can be simplified to

if (c == ' ' || c == '\n' || c == '\t')
 nb = nb + 1;
else
 nb = 0;

The output of the program still contains all kind of whitespace (space, newline or tab character), because the first whitespace character in a sequence is preserved. This means that

a<space><NL>b is transformed to a<space>b
a<NL><space>b is transformed to a<NL>b

It might be a good idea to replace all whitespace sequences by a space character:

if (c == ' ' || c == '\n' || c == '\t') {
 if (nb == 0) {
 putchar(' ');
 }
 nb = nb + 1;
} else {
 nb = 0;
 putchar(c);
}

You can also use isspace() to check for whitespace characters (but note that this function is locale-dependent).

syb0rg's suggested already to initialize all variables. In particular with the initialization

int nb = 0;

the check if (nb >= 1) after finding a space character is not necessary anymore, and that part can be simplified to

if (c == ' ' || c == '\n' || c == '\t')
 nb = nb + 1;
else
 nb = 0;

syb0rg suggested already to initialize all variables. In particular with the initialization

int nb = 0;

the check if (nb >= 1) after finding a space character is not necessary anymore, and that part can be simplified to

if (c == ' ' || c == '\n' || c == '\t')
 nb = nb + 1;
else
 nb = 0;

The output of the program still contains all kind of whitespace (space, newline or tab character), because the first whitespace character in a sequence is preserved. This means that

a<space><NL>b is transformed to a<space>b
a<NL><space>b is transformed to a<NL>b

It might be a good idea to replace all whitespace sequences by a space character:

if (c == ' ' || c == '\n' || c == '\t') {
 if (nb == 0) {
 putchar(' ');
 }
 nb = nb + 1;
} else {
 nb = 0;
 putchar(c);
}

You can also use isspace() to check for whitespace characters (but note that this function is locale-dependent).

Source Link
Martin R
  • 24.2k
  • 2
  • 38
  • 96
Loading
lang-c

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