Search code examples
cc89ansi-c

Remove white chars between commas, but not between what inside the commas


I'm new to C and learning C90. I'm trying to parse a string into a command, But I have a hard time trying to remove white chars.

My goal is to parse a string like this:

NA ME, NAME   , 123 456, 124   , 14134, 134. 134   ,   1   

into this:

NA ME,NAME,123 456,124,14134,134. 134,1

so the white chars that were inside the arguments are still there, but the other white chars are removed.

I thought about using strtok, but I still want to keep the commas, even if there are multiple consecutive commas.

Until now I used:

void removeWhiteChars(char *s)
{
    int i = 0;
    int count = 0;
    int inNum = 0;
    while (s[i])
    {
        if (isdigit(s[i]))
        {
            inNum = 1;
        }
        if (s[i] == ',')
        {
            inNum = 0;
        }
        if (!isspace(s[i]) && !inNum)
            s[count++] = s[i];
        else if (inNum)
        {
            s[count++] = s[i];
        }

        ++i;
    }
    s[count] = '\0'; /* adding NULL-terminate to the string */
}

But it only skips for numbers and does not remove white chars after the number until the comma, and it's quite wrong.

i would appreciate any kind of help, I'm stuck on this one for two days now.


Solution

  • You need to do lookaheads whenever you encounter possible skippable whitespace. The function below, every time it sees a space, checks forward if it ends with a comma. Likewise, for every comma, it checks and removes all following spaces.

    // Remove elements str[index] to str[index+len] in place
    void splice (char * str, int index, int len) {
      while (str[index+len]) {
        str[index] = str[index+len];
        index++;
      }
      str[index] = 0;
    }
    
    void removeWhiteChars (char * str) {
      int index=0, seq_len;
    
      while (str[index]) {
        if (str[index] == ' ') {
          seq_len = 0;
    
          while (str[index+seq_len] == ' ') seq_len++;
    
          if (str[index+seq_len] == ',') {
            splice(str, index, seq_len);
          }
        }
        if (str[index] == ',') {
          seq_len = 0;
          while (str[index+seq_len+1] == ' ') seq_len++;
    
          if (seq_len) {
            splice(str, index+1, seq_len);
          }
        }
        index++;
      }
    }