Problem of the Day
A new programming or logic puzzle every Mon-Fri

Colored Text

A lost art in today's world is color coded text on the command line/terminal. So today's problem is to create a program that prints out the string "Hello World" where the words are two different colors (such as red and blue). Feel free to use third party libraries if it will help.

Permalink: http://problemotd.com/problem/colored-text/

Comments:

  • David - 9 years, 2 months ago

    This has proven to be quite useful for this task in c.
    http://stackoverflow.com/questions/3585846/color-text-in-terminal-aplications-in-unix

    #include <stdio.h>
    
    #define KNRM "\x1B[0m"
    #define KRED  "\x1B[31m"
    #define KGRN  "\x1B[32m"
    #define KYEL  "\x1B[33m"
    #define KBLU  "\x1B[34m"
    #define KMAG  "\x1B[35m"
    #define KCYN  "\x1B[36m"
    #define KWHT  "\x1B[37m"
    
    int main()
    {
      printf(KRED "Hello ");
      printf(KBLU "World\n");
    
      return 0;
    }
    

    My cygwin terminal displays it correctly. cmd hates it. I leave it as a further exercise to the reader to find out about background colors for the text too. I tried some of them at one point when configuring my cygwin terminal, but don't care enough to find it now.

    reply permalink

Content curated by @MaxBurstein