28 29 30 31
|
if(i != 0)
{
spaces++;
}
|
What is this supposed to be doing? I starts at 1 and goes up in the loop at line 23, so it will never be 0, so this loop always executes.
You need to test your code with a number of scenarios.
- an empty string
- 1 word with no space before or after
- 1 word with space(s) before, none after
- 1 word with no space before, space(s) after
- 1 word with space(s) before and after
- The previous 4 tests with 2 word separated by space(s).
This will make sure your algorithm handles all scenarios properly.
As far as rating your coding...
It looks pretty good to me. My comments would be:
22 23
|
int spaces = 0, i;
for(i = 1; user_string[i] != '\0'; i++)
|
I personally don't like declaring 2 variables in the same line (line 22)--it gets kind of confusing.
If you don't need to use 'i' after the loop (after you figure out what you're doing at line 28), you can declare it within the for statement
for(int i = 1; user_string[i] != '\0'; i++)