Home > OS >  How would I center a multiline ascii art
How would I center a multiline ascii art

Time:02-10

I'm new to C I have a ASCII art that looks like this

    string title =
        "  _____ ____  _   _  _____ _      ______  _____ _____ _ \n"
        " / ____/ __ \\| \\ | |/ ____| |    |  ____|/ ____/ ____| |\n"
        "| |   | |  | |  \\| | (___ | |    | |__  | (___| (___ | |\n"
        "| |   | |  | | . ` |\\___ \\| |    |  __|  \\___ \\\\___ \\| |\n"
        "| |___| |__| | |\\  |____) | |____| |____ ____) |___) |_|\n"
        " \\_____\\____/|_| \\_|_____/|______|______|_____/_____/(_)\n";

I want to paste this ASCII art in the console but I don't want it to be on the left side I want it to be centered how would I go about centering this ASCII art in the console.

CodePudding user response:

Alright I found this solution where you can make a array of the lines of the ascii art and then add 50 spaces before each line.

int main()
{
    string title[6] = {"  _____ ____  _   _  _____ _      ______  _____ _____ _ ", " / ____/ __ \\| \\ | |/ ____| |    |  ____|/ ____/ ____| |", "| |   | |  | |  \\| | (___ | |    | |__  | (___| (___ | |", "| |   | |  | | . ` |\\___ \\| |    |  __|  \\___ \\\\___ \\| |", "| |___| |__| | |\\  |____) | |____| |____ ____) |___) |_|", " \\_____\\____/|_| \\_|_____/|______|______|_____/_____/(_)"};

    for (size_t i = 0; i < 6; i  )
        cout << string(50, ' ')   title[i] << endl;

    return 0;
}

CodePudding user response:

Here's an example if you're using fmt with center alignment.


#include <string>
#include <fmt/core.h>

void print(std::string s){
    int width = 100;
    fmt::print("{:*^{}}\n", s, width); // '*' fills the blank space so it's easier to see the center alignment effect.
}

int main(){
    print("  _____ ____  _   _  _____ _      ______  _____ _____ _ ");
    print(" / ____/ __ \\| \\ | |/ ____| |    |  ____|/ ____/ ____| |");
    print("| |   | |  | |  \\| | (___ | |    | |__  | (___| (___ | |");
    print("| |   | |  | | . ` |\\___ \\| |    |  __|  \\___ \\\\___ \\| |");
    print("| |___| |__| | |\\  |____) | |____| |____ ____) |___) |_|");
    print(" \\_____\\____/|_| \\_|_____/|______|______|_____/_____/(_)");
    
    
    return 0;
}


Synax for fmt

Live Demo

Output

**********************  _____ ____  _   _  _____ _      ______  _____ _____ _ **********************
********************** / ____/ __ \| \ | |/ ____| |    |  ____|/ ____/ ____| |**********************
**********************| |   | |  | |  \| | (___ | |    | |__  | (___| (___ | |**********************
**********************| |   | |  | | . ` |\___ \| |    |  __|  \___ \\___ \| |**********************
**********************| |___| |__| | |\  |____) | |____| |____ ____) |___) |_|**********************
********************** \_____\____/|_| \_|_____/|______|______|_____/_____/(_)**********************
  •  Tags:  
  • c
  • Related