Skip to content

Instantly share code, notes, and snippets.

@kkd927
Created June 6, 2014 12:52
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 kkd927/80c09f5669ac69679131 to your computer and use it in GitHub Desktop.
Save kkd927/80c09f5669ac69679131 to your computer and use it in GitHub Desktop.
Code to erase blank before and after string in C/C++
char * space_remove(char *str)
{
// change BUF_SIZE depending on the length of your string
char buf[BUF_SIZE]={0,};
char *p;
p = strtok(str, "\r\n\t ");
strcat(buf, p);
while(p!=NULL){
p = strtok(NULL, "\r\n\t ");
if(p!=NULL) {
strcat(buf, " ");
strcat(buf, p);
}
}
memset(str, '\0', BUF_SIZE);
strcpy(str, buf);
return str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment