Skip to content

Instantly share code, notes, and snippets.

@shihongzhi
Created March 29, 2012 08:27
Show Gist options
  • Save shihongzhi/2234958 to your computer and use it in GitHub Desktop.
Save shihongzhi/2234958 to your computer and use it in GitHub Desktop.
remove_multiple_spaces; answer to https://gist.github.com/2227226/
#include <stdio.h>
int remove_multiple_spaces(char str[]){
char *result;
char *cur;
result = cur = str;
while (*cur) {
if (*cur==' ') {
*result++ = *cur++;
while (*cur == ' ') {
cur++;
}
}
else{
*result++ = *cur++;
}
}
*result = 0;
return (result - str);
}
int main (int argc, const char * argv[])
{
char s1[] = "abc def";
char s2[] = " abc def ghi";
char s3[] = " a b d e ";
char s4[] = " ";
printf("%d,%s\n", remove_multiple_spaces(s1), s1);
printf("%d,%s\n", remove_multiple_spaces(s2), s2);
printf("%d,%s\n", remove_multiple_spaces(s3), s3);
printf("%d,%s\n", remove_multiple_spaces(s4), s4);
return 0;
}
@shihongzhi
Copy link
Author

面试的时候可以写一些unit test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment