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';
}