0

Is there a way to color just one element in an array?

Say, I have a 2D array like this:

main ()
{
int x,y;
char arr[3][3];
for (x=0;x<3;x++) 
for (y=0;y<3;y++) 
 arr[x][y]='a';

arr[1][1]='b';
for (x=0;x<3;x++) 
for (y=0;y<3;y++)
printf("%c", arr[x][y]);
}

How to apply a color to only character 'b' located at arr [1][1]?

1 Answers1

0

I found solution. This one's for Windows. If you have the same situation as I do, here's how I did it. I used the SetConsoleTextAttribute() function, thanks to good answer in this question

so my code was

#include <windows.h>
#include <stdio.h>

main
{
enter code here
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
WORD saved_attributes;
char arr[3][3];
for (x=0;x<3;x++) 
for (y=0;y<3;y++) 
arr[x][y]='a';
arr[1][1]='b';
for (x=0;x<3;x++)
for (y=0;y<3;y++)
{
if (arr[x][y]=='b')
{
SetConsoleTextAttribute(hConsole, FOREGROUND_RED);
printf("%c", arr[x][y]);
SetConsoleTextAttribute(hConsole, saved_attributes);
}
else 
{
printf("%c", arr[x][y]);
}
}
Community
  • 1
  • 1