Rusu Florin Andrei
Rusu Florin Andrei

Reputation: 49

Changing the color of a specific character

Is there any way to change the color in the console of a specific character? I'm using code blocks and, for example, I want to change the color of all @ to red and all o to yellow.

Upvotes: 1

Views: 6110

Answers (1)

Shvet Chakra
Shvet Chakra

Reputation: 1043

You have to write a different function to achieve this task. I am adding a code to show how it can be done in C.`

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

void output(char *s)
{
int i=0;
while(*(s+i) !='\0')
{
    if(*(s+i)=='@')
   {
   textcolor(RED);
    cprintf("%c",*(s+i));
   }
   else if(*(s+i) =='.')
   {
    textcolor(YELLOW);
    cprintf("%c",*(s+i));
   }
   else
   {
   textcolor(WHITE);
   cprintf("%c",*(s+i));
   }
   i++;
}
}
void main()
{
char S[]="@shvet.";
output(S);
getch();
}

Here is the image for the output console window. Output

Note that I have used cprintf function instead of printf. This is because cprintf send formatted output to text window on screen and printf sends it to stdin.

Upvotes: 1

Related Questions