Monday, September 26, 2011

Squeeze all multiple spaces in a string into one space

 

#include <stdio.h>

void trimspace(char *dst) {

const char *src = dst;

int tocopy = 1;

char c;

while((c = *src++)) {

if(tocopy)
*dst++ = c;

tocopy = (c != ' ') || (*src != ' ');
}

*dst = '\0';
}


int main() {
char s[64];

scanf("%[^\n]c", s);

trimspace(s);

printf("%s\n", s);
}

0 comments:

Post a Comment