Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print "HELLO" in big block letters?

Tags:

c++

How to print "HELLO" in big block letters where each letter is 7 characters high and 5 characters wide?

I thought of printing like this:

H   H    EEEEE   L       L        OOOOO
H   H    E       L       L       O     O
H   H    E       L       L       O     O
HHHHH    EEEEE   L       L       O     O
H   H    E       L       L       O     O
H   H    E       L       L       O     O
H   H    EEEEE   LLLLL   LLLLL    OOOOO

But I don't know how to print one character beside the other. I have tried to print H and E, but they are not horizontal. They came vertical.

Here is my code:

int problem4_4()
{
    int i;

    for(i=0;i<7;++i)
    {
        if(i==3)
            cout<<"HHHHH\n";
        else
            cout<<"H   H\n";
    }

    for(i=7;i<14;i++)
    {
        if(i==7||i==10||i==13)
            cout<<"EEEEE\n";
        else
            cout<<"E\n";
    }

    return 0;
}
like image 322
sindhu jampani Avatar asked Nov 17 '15 23:11

sindhu jampani


3 Answers

Here is example of the method @Kevin suggested

std::vector<std::string> v;

v.push_back("H   H    EEEEE   L       L        OOOOO");
v.push_back("H   H    E       L       L       O     O");
v.push_back("H   H    E       L       L       O     O");
v.push_back("HHHHH    EEEEE   L       L       O     O");
v.push_back("H   H    E       L       L       O     O");
v.push_back("H   H    E       L       L       O     O");
v.push_back("H   H    EEEEE   LLLLL   LLLLL    OOOOO");

for (auto row : v)
{
    for (auto col : row)
        cout << col;
    cout << endl;
}

Edit:
Per suggestions in comment section, you can draw the letters individually, then add the letters from left to right, put everything in result and print

void add(std::vector<std::string> &result, const std::vector<std::string> &letter)
{
    for (unsigned i = 0; i < letter.size(); i++)
    {
        if (i >= result.size())
            result.push_back("");
        result[i] += letter[i];
    }
}

int main(void)
{
    std::vector<std::string> H, E, L, O;

    H.push_back("H   H ");
    H.push_back("H   H ");
    H.push_back("H   H ");
    H.push_back("HHHHH ");
    H.push_back("H   H ");
    H.push_back("H   H ");
    H.push_back("H   H ");

    E.push_back("EEEEE ");
    E.push_back("E     ");
    E.push_back("E     ");
    E.push_back("EEEEE ");
    E.push_back("E     ");
    E.push_back("E     ");
    E.push_back("EEEEE ");

    L.push_back("L     ");
    L.push_back("L     ");
    L.push_back("L     ");
    L.push_back("L     ");
    L.push_back("L     ");
    L.push_back("L     ");
    L.push_back("LLLLL ");

    O.push_back(" OOOOO  ");
    O.push_back("O     O ");
    O.push_back("O     O ");
    O.push_back("O     O ");
    O.push_back("O     O ");
    O.push_back("O     O ");
    O.push_back(" OOOOO  ");

    std::vector<std::string> result;
    add(result, H);
    add(result, E);
    add(result, L);
    add(result, L);
    add(result, O);

    for (unsigned row = 0; row < result.size(); row++)
        std::cout << result[row].c_str() << std::endl;

    return 0;
}
like image 159
Barmak Shemirani Avatar answered Oct 05 '22 03:10

Barmak Shemirani


For a hint: each letter is the same size, so make yourself an array of "letters": each is a 2D array of characters that form your letter.

Now, when you go to print, say, "Hello", you can print the first line of each letter, then a newline, then the second line of each letter, then another newline, etc.

Hope this helps.

like image 35
Dúthomhas Avatar answered Oct 05 '22 03:10

Dúthomhas


Here’s a version that is slightly complicated for an example for a beginner, but not too complicated. It will print arbitrary strings (call it with a command-line argument) and can trivially be extended to new glyphs and even new encodings. It stores the glyph information in an unordered_map, i.e. a hash table:

#include <array>
#include <cstdlib>
#include <iostream>
#include <string>
#include <unordered_map>

using std::cout;

using ctype = char;
static constexpr unsigned rows = 7;
using letter_t = std::array< std::string, rows >;
using glyph_table = std::unordered_map< ctype, letter_t >;

const glyph_table& create_glyphs()
{
  // Returns a reference to a singleton.
  static glyph_table glyphs;
  static bool ran = false;

  if (!ran) { // Skip this step if called again.
    glyphs.emplace( 'H', letter_t({
      "H   H",
      "H   H",
      "H   H",
      "HHHHH",
      "H   H",
      "H   H",
      "H   H" })
    );
    glyphs.emplace( 'E', letter_t({
      "EEEEE",
      "E    ",
      "E    ",
      "EEEEE",
      "E    ",
      "E    ",
      "EEEEE" })
    );
    glyphs.emplace( 'L', letter_t({
      "L    ",
      "L    ",
      "L    ",
      "L    ",
      "L    ",
      "L    ",
      "LLLLL" })
    );
    glyphs.emplace( 'O', letter_t({
      " OOO ",
      "O   O",
      "O   O",
      "O   O",
      "O   O",
      "O   O",
      " OOO "})
    );
    glyphs.emplace( 0, letter_t({
      "?  ??",
      "??? ?",
      "??? ?",
      "?? ??",
      "?? ??",
      "?????",
      "?? ??" })
    );

    ran = true;
  } // end if

  return glyphs;
}

int main( int argc, const char *argv[] )
{
  const char* const message = (argc > 1) ? argv[1] : "HELLO";
  const glyph_table& glyphs = create_glyphs();

  for ( unsigned i = 0; i < rows; ++i ) {
    for ( unsigned j = 0; j < 22 && message[j]; ++j ) {
      // Look up the glyph for the jth character of message, or if that isn't in the table, the glyph stored under the null character.
      const letter_t& g = glyphs.count(message[j]) ? glyphs.find(message[j])->second : glyphs.find(0)->second;
      /* The syntax to get a reference to the value indexed by a key from an iterator returned by find() is a little baroque for a STL hash table.
       */
      // Print its ith row:
      cout << ' ' << g[i];
    }
    cout << '\n';
  }

  return EXIT_SUCCESS;
}

It would be more efficient in terms of execution time (but not memory) to loop over the characters of the message first, and generate all the rows before printing them. You can see Barmak Shemirani’s solution for a hint how to do so.

like image 36
Davislor Avatar answered Oct 05 '22 02:10

Davislor