I am currently printing a string using printf("'%.*s'\n", length, start);
, where start
is a const char*
and length is an int
.
The string being printed sometimes contains newline characters \n
which mess up the formatting, is it possible for them to be replaced with the characters \
and n
in the printed output, instead of the \n
character.
If not, could you help with a function that would replace the characters?
The string is a malloc'd string that is has other references from different pointers so it cannot be changed.
EDIT: I have written the following code which I think does what I need it to
static void printStr(const char* start, int length) {
char* buffer = malloc(length * sizeof(char));
int processedCount = 0;
for(int i = 0; i < length; i++) {
char c = start[i];
if(c == '\n') {
printf("%.*s\\n", processedCount, buffer);
processedCount = 0;
} else {
buffer[processedCount] = c;
processedCount++;
}
}
printf("%.*s", processedCount, buffer);
free(buffer);
}