Created
March 29, 2012 08:27
-
-
Save shihongzhi/2234958 to your computer and use it in GitHub Desktop.
remove_multiple_spaces; answer to https://gist.github.com/2227226/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
面试的时候可以写一些unit test