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
The following code modifies the string in place; if you don't want to destroy your original input, you can pass a second buffer to receive the modified string. Should be fairly self-explanatory:
#include
#include
char *squeeze(char *str)
{
int r; /* next character to be read */
int w; /* next character to be written */
r=w=0;
while (str[r])
{
if (isspace(str[r]) || iscntrl(str[r]))
{
if (w > 0 && !isspace(str[w-1]))
str[w++] = ' ';
}
else
str[w++] = str[r];
r++;
}
str[w] = 0;
return str;
}
int main(void)
{
char test[] = "\t\nThis\nis\ta\b test.";
printf("test = %s\n", test);
printf("squeeze(test) = %s\n", squeeze(test));
return 0;
}