Skip to content

Instantly share code, notes, and snippets.

@mygnu
Last active January 3, 2016 13:09
Show Gist options
  • Save mygnu/8467443 to your computer and use it in GitHub Desktop.
Save mygnu/8467443 to your computer and use it in GitHub Desktop.
removing all spaces form a string
int
main(int argc, char *argv[])
{
char mystr[50] = {"The brown fox."};
//gets(mystr);
char result[50];// = "______________";
printf("%s \n", mystr);
removeXtraSpaces(mystr, result);
printf("%s\n", result);
}
void removeXtraSpaces(char str[], char nstr[])
{
int flag = NONSPACE;
while (*str != '\0')
{
if(*str != ' ')
{
flag = NONSPACE;
*nstr = *str;
nstr++;
}
else if (*str == ' ' && flag == NONSPACE)
{
flag = SPACE;
*nstr = ' ';
nstr++;
}
str++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment