I have read a few lines of text into an array of C-strings. The lines have an arbitrary number of tab or space-delimited columns, and I am trying to figure out how to remove
I made a small improvment over John Bode's to remove trailing whitespace as well:
#include
char *squeeze(char *str)
{
char* r; /* next character to be read */
char* w; /* next character to be written */
char c;
int sp, sp_old = 0;
r=w=str;
do {
c=*r;
sp = isspace(c);
if (!sp) {
if (sp_old && c) {
// don't add a space at end of string
*w++ = ' ';
}
*w++ = c;
}
if (str < w) {
// don't add space at start of line
sp_old = sp;
}
r++;
}
while (c);
return str;
}
#include
int main(void)
{
char test[] = "\t\nThis\nis\ta\f test.\n\t\n";
//printf("test = %s\n", test);
printf("squeeze(test) = '%s'\n", squeeze(test));
return 0;
}
br.