Skip to content

Instantly share code, notes, and snippets.

@chenshuo
Created March 29, 2012 03:25
Show Gist options
  • Save chenshuo/2232954 to your computer and use it in GitHub Desktop.
Save chenshuo/2232954 to your computer and use it in GitHub Desktop.
Remove continuous spaces, answers gist.github.com/2227226
#include <algorithm>
#include <string.h>
struct AreBothSpaces
{
bool operator()(char x, char y) const
{
return x == ' ' && y == ' ';
}
};
int removeContinuousSpaces(char* const str)
{
size_t len = strlen(str); // or use std::string
char* end = str+len;
assert(*end == '\0');
char* last = std::unique(str, end, AreBothSpaces());
*last = '\0';
return last - str;
}
int main()
{
char inout[256] = "";
strcpy(inout, "");
removeContinuousSpaces(inout);
assert(strcmp(inout, "") == 0);
strcpy(inout, "a");
removeContinuousSpaces(inout);
assert(strcmp(inout, "a") == 0);
strcpy(inout, " a");
removeContinuousSpaces(inout);
assert(strcmp(inout, " a") == 0);
strcpy(inout, " a");
removeContinuousSpaces(inout);
assert(strcmp(inout, " a") == 0);
strcpy(inout, "a ");
removeContinuousSpaces(inout);
assert(strcmp(inout, "a ") == 0);
strcpy(inout, "a ");
removeContinuousSpaces(inout);
assert(strcmp(inout, "a ") == 0);
strcpy(inout, "abc def");
removeContinuousSpaces(inout);
assert(strcmp(inout, "abc def") == 0);
strcpy(inout, "abc def ghi");
removeContinuousSpaces(inout);
assert(strcmp(inout, "abc def ghi") == 0);
strcpy(inout, " a b d e ");
removeContinuousSpaces(inout);
assert(strcmp(inout, " a b d e ") == 0);
strcpy(inout, " ");
removeContinuousSpaces(inout);
assert(strcmp(inout, " ") == 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment