Skip to content

Instantly share code, notes, and snippets.

@JosephPecoraro
Created August 4, 2008 17:57
Show Gist options
  • Save JosephPecoraro/3940 to your computer and use it in GitHub Desktop.
Save JosephPecoraro/3940 to your computer and use it in GitHub Desktop.
#include <stdio.h>
void str_squeeze(char *s) {
int i, j;
char last = '\0';
for ( i = j = 0; s[i]; last = s[i++] )
if ( s[i] != last )
s[j++] = s[i];
s[j] = '\0';
}
void str_squeeze_char(char *s, char c) {
int i, j, used;
for ( i = j = used = 0; s[i]; ++i ) {
if ( !used && s[i] == c ) {
s[j++] = s[i];
used = 1;
} else if ( s[i] != c ) {
s[j++] = s[i];
used = 0;
}
}
s[j] = '\0';
}
int main() {
char s[10] = "testtiing";
printf("%s\n", s);
str_squeeze(s);
printf("%s\n", s);
char s2[10] = "testtiing";
printf("%s\n", s2);
str_squeeze_char(s2, 'i');
printf("%s\n", s2);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment