Beginner at C here.
I understand that strstr()
can be used for finding if strings contain a certain substring and that printf()
can display colored output (as explained here: stdlib and colored output in C).
What I am trying to figure out is a simple way to color only the matching portion of the string (like in grep
), since strstr()
returns the entire part of the line from the match. Alternatively I could print the entire matching line but that would end up with the entire line being colored.
Assuming that bold = coloring for illustrative purposes, my desired result when searching for the substring elephant is:
this is an elephant's tusk
printf + coloring the strstr()
output gives:
elephant's tusk
and of course printf + coloring the entire matched line gives:
this is an elephant's tusk
Thank you in advance for the help.
The easiest solution is to break the printing process into three pieces. If strstr returns a non-NULL result, print the number of characters from the start of the initial string until reaching the strstr result. Then color the strstr result until the length of the "needle" is matched. Then print the uncolored remainder of the haystack:
const char *color_start, *color_end;
print_head = haystack;
color_start = strstr (haystack, needle);
color_end = color_start + strlen (needle) - 1;
while (*print_head) {
if (print_head == color_start) { /* init color change */ }
else if (print_head == color_end) { /* revert color change */ }
putchar (*print_head++);
}
A more efficient version would be:
const char *color_start, *color_end;
print_head = haystack;
color_start = strstr (haystack, needle);
color_end = color_start + strlen (needle) - 1;
while (print_head < color_start)
putchar (*print_head++);
/* init color change */
while (print_head <= color_end)
putchar (*print_head++);
/* revert color change */
while (*print_head)
putchar (*print_head++);
It is more efficient because it tests only one condition through each loop, rather than up to three conditions per loop.