Mega Code Archive

 
Categories / C / Code Snippets
 

Removing spaces and puctuation from a string

#include <stdio.h> #include <ctype.h> int main() { char buffer[100] = "I Love Clementine"; char *pbuffer1 = buffer; char *pbuffer2 = buffer; pbuffer1 = buffer; /* Reset pointer to start */ while(*pbuffer1 != '\0') { if(ispunct(*pbuffer1) || isspace(*pbuffer1)) { ++pbuffer1; continue; } else *pbuffer2++ = *pbuffer1++; /* otherwise, copy the character */ } *pbuffer2 = '\0'; /* Append string terminator */ printf("\n%s\n", buffer); }