patterncMinor
Cut excess whitespace from input and print to output
Viewed 0 times
whitespacecutandoutputinputprintexcessfrom
Problem
Title is pretty self explanatory, this program just takes in input (not from command line arguments), removes the extra spaces in the input, then prints that to output.
Any suggestions on how to improve it?
/*replace string of blanks with single blank*/
#include "stdio.h"
int main(int argc, char const *argv[])
{
int nb,c = 0;
while ((c=getchar()) != EOF){
if (c == ' ' || c == '\n' || c == '\t')
if (nb >= 1)
nb = nb + 1;
else
nb = 1;
else
nb = 0;
if (nb <= 1)
putchar(c);
}
return 0;
}Any suggestions on how to improve it?
Solution
A few suggestions:
-
Extract the space removal to a function of its own. It a repeatable task and despite this being a short program it's still good practice. Then all you have to do in
Keep in mind that this method modifies the original string.
-
Change how you get input from the user. I'd recommend either getting input via command line arguments, or using
-
If you decide to not use command line arguments, declare your function as:
-
Put the variable declarations to separate lines and initialize them to some value. From Code Complete, 2d Edition, p. 759:
With statements on their own lines, the code reads from top to bottom,
instead of top to bottom and left to right. When you’re looking for a
specific line of code, your eye should be able to follow the left
margin of the code. It shouldn’t have to dip into each and every line
just because a single line might contain two statements.
-
You don't have to return
C99 & C11 §5.1.2.2(3)
...reaching the
value of
-
Extract the space removal to a function of its own. It a repeatable task and despite this being a short program it's still good practice. Then all you have to do in
main() is just pass in the input string. Here's a function I came up with:void removeSpaces(char* source)
{
for (char *i = source, *j = source; *j != 0;)
{
*i = *j++;
if(*i != ' ') i++;
}
*i = 0;
}Keep in mind that this method modifies the original string.
-
Change how you get input from the user. I'd recommend either getting input via command line arguments, or using
fgets(). It's safer those ways and easier to do in my opinion. -
If you decide to not use command line arguments, declare your function as:
int main(void)-
Put the variable declarations to separate lines and initialize them to some value. From Code Complete, 2d Edition, p. 759:
With statements on their own lines, the code reads from top to bottom,
instead of top to bottom and left to right. When you’re looking for a
specific line of code, your eye should be able to follow the left
margin of the code. It shouldn’t have to dip into each and every line
just because a single line might contain two statements.
-
You don't have to return
0 at the end of main(), just like you wouldn't bother putting return; at the end of a void-returning function. The C standard knows how frequently this is used, and lets you not bother.C99 & C11 §5.1.2.2(3)
...reaching the
} that terminates the main() function returns avalue of
0.Code Snippets
void removeSpaces(char* source)
{
for (char *i = source, *j = source; *j != 0;)
{
*i = *j++;
if(*i != ' ') i++;
}
*i = 0;
}int main(void)Context
StackExchange Code Review Q#115640, answer score: 4
Revisions (0)
No revisions yet.