?
- .
void Test(const char *input, const char *expected_output) {
char buffer[80];
strcpy(buffer, input);
RemoveSpace(buffer);
assert(strcmp(buffer, expected_output) == 0);
}
int main() {
Test(" Leading spaces removed.", "Leading spaces removed.");
Test("Trailing spaces removed. ", "Trailing spaces removed.");
Test("Inner spaces trimmed.", "Inner spaces trimmed.");
Test(" A little of everything. ", "A little of everything.");
Test(" \tTabs \t\tare \t spaces, too.", "Tabs are spaces, too.");
return 0;
}
OP , .
, ?
. , .
C- C , . , , . , , , , .
:
#include <assert.h>
#include <ctype.h>
#include <string.h>
void RemoveSpace(char *string) {
char *target = string;
char *last = target;
int skipping_spaces = 1;
for (const char *source = string; *source != '\0'; ++source) {
if (isspace(*source)) {
if (!skipping_spaces) {
*target++ = *source;
skipping_spaces = 1;
}
} else {
*target++ = *source;
last = target;
skipping_spaces = 0;
}
}
*last = '\0';
}
, , , , , . , ( ).