Created
March 29, 2012 05:26
-
-
Save passos/2233688 to your computer and use it in GitHub Desktop.
Remove multiple spaces, answers gist.github.com/2227226
This file contains 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 removeMultipleSpace(char baseStr[]) | |
{ | |
char *p = baseStr; | |
char *t = baseStr; | |
while ( *p ) { | |
if ( *p == ' ' ) { | |
*t++ = *p++; | |
while ( *p && (*p == ' ') ) p++; | |
} else { | |
*t++ = *p++; | |
} | |
} | |
*t = 0; | |
return t - baseStr; | |
} | |
int main() | |
{ | |
char s1[] = "abc def"; | |
char s2[] = " abc def ghi"; | |
char s3[] = " a b d e "; | |
char s4[] = " "; | |
printf("%d |%s|\n", removeMultipleSpace(s1), s1); | |
printf("%d |%s|\n", removeMultipleSpace(s2), s2); | |
printf("%d |%s|\n", removeMultipleSpace(s3), s3); | |
printf("%d |%s|\n", removeMultipleSpace(s4), s4); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment