79404614

Date: 2025-02-01 07:56:28
Score: 2
Natty:
Report link

Is there any way to do this without making a new string, or is there a way to change the pointer address of s to point to newstr?

Yes, you can refer to functions, e.g., canonicalize_newline, remove_backslash_newline, ... etc., in chibicc repo, which handle tokenization without creating a new string.

// Replaces \r or \r\n with \n.
static void canonicalize_newline(char *p) {
  int i = 0, j = 0;

  while (p[i]) {
    if (p[i] == '\r' && p[i + 1] == '\n') {
      i += 2;
      p[j++] = '\n';
    } else if (p[i] == '\r') {
      i++;
      p[j++] = '\n';
    } else {
      p[j++] = p[i++];
    }
  }

  p[j] = '\0';
}

Try using a similar approach by employing two pointers, i for reading and j for writing, to process the string in place.

static void remove_extra_whitespace(char *p) {
    int i = 0, j = 0;
    int space_found = 0;

    while (p[i]) {
        if (isspace(p[i])) {  
            if (!space_found) {  
                p[j++] = ' ';
                space_found = 1; 
            }
        } else {
            p[j++] = p[i];
            space_found = 0;
        }
        i++;
    }

    if (j > 0 && p[j - 1] == ' ')
        j--;

    p[j] = '\0';
}
Reasons:
  • Blacklisted phrase (1): is there a way
  • Blacklisted phrase (1): Is there any
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Starts with a question (0.5): Is there any
  • Low reputation (0.5):
Posted by: Shelton Liu