Skip to content

Instantly share code, notes, and snippets.

@passos
Created March 29, 2012 05:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save passos/2233688 to your computer and use it in GitHub Desktop.
Save passos/2233688 to your computer and use it in GitHub Desktop.
Remove multiple spaces, answers gist.github.com/2227226
#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