This is a function of Linux grep command, (It basically search for a specific word and prints each line that has that word)
How can I modify this function to color the word that I'm looking for in "red color" ? (I want to color the word only, not the whole sentence)
#include<stdio.h>
#include<string.h>
void main()
{
char fn[10],pat[10],temp[200];
FILE *fp;
printf("Enter file name\n");
scanf("%s",fn);
printf("Enter pattern to be searched\n");
scanf("%s",pat);
fp=fopen(fn,"r");
while(!feof(fp))
{
fgets(temp,1000,fp);
if(strstr(temp,pat))
printf("%s",temp);
}
fclose(fp);
}
I can add colors
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_GREEN "\x1b[32m"
#define ANSI_COLOR_YELLOW "\x1b[33m"
#define ANSI_COLOR_BLUE "\x1b[34m"
But, how can I design the logic of splitting each line into words and then coloring each word that match my input without using strtok(), I tried to expand
if(strstr(temp,pat))
printf("%s",temp);
and to add an array newString[] and match it with
temp[] for i and j to check if newString[] is equivalent to pat[] or not but that didn't work