Objective
Write a C# program to imitate the basic Unix SysV "banner" utility, able to display big texts.
Example C# Exercise
Show C# Code
using System; // Import the System namespace for basic functionality
class Program // Define the main class
{
static void Main() // The entry point of the program
{
Console.Write("Enter the text to display in a banner: "); // Prompt the user to input the text
string input = Console.ReadLine(); // Read the input from the user
// Call the method to display the banner with the input text
DisplayBanner(input);
}
// Method to display a banner with the given text
static void DisplayBanner(string text)
{
// Define the font for the banner using big letters
string[] letters = new string[]
{
"A B C D E F G H I J K L M N O P Q R S T U V W X Y Z",
"A A BBBBB CCCCC D D EEEEE FFFFF G G H H I J K K L M M N N O O P P Q Q R R S S T T U U V V W W X X Y Y Z Z",
"A A B B C D D E F G G H H I J K K L MM MM NN NN OO OO PP PP QQ QQ RR RR S T T U U V V W W X X Y Y Z",
"AAAAA BBBBB C D D EEEE FFFFF GGGGG HHHHH I J KKKK L M M M N N O O PPPP Q Q RRRR SSSS T T U U V V W W X X Y Y Z",
"A A B B C D D E F G G H H I J K K L M M NN NN OO OO PP P Q Q RR RR S T T U U V V W W X X Y Y Z",
"A A BBBBB CCCCC DDDDD EEEEE F G G H H I J K K LLLLL M M N N O O P P Q Q R R SSSS T T UUUU V V W W X X Y Y Z Z"
};
// Loop through each row in the big letters to create the banner
for (int i = 0; i < 6; i++) // We have 6 lines for the letters
{
foreach (char c in text.ToUpper()) // Iterate through each character in the input text (convert to uppercase)
{
if (c == ' ') // If the character is a space, just print a space in the banner
{
Console.Write(" "); // Adjust space size for proper alignment
}
else if (c >= 'A' && c <= 'Z') // Check if the character is a valid letter (A-Z)
{
// Calculate the position of the character in the alphabet (0-25)
int charIndex = c - 'A';
// Calculate the starting index for the letter in the string (each letter is 6 characters long)
int startIndex = charIndex * 6;
// Check if the startIndex is within bounds of the current row string
if (startIndex + 6 <= letters[i].Length)
{
// Print the corresponding row of the letter in the banner
Console.Write(letters[i].Substring(startIndex, 6)); // Use length 6 to include the space
}
}
else
{
// If the character is invalid (not A-Z), just print spaces
Console.Write(" ");
}
}
Console.WriteLine(); // Move to the next line in the banner
}
}
}
Output
Enter the text to display in a banner: The final
V W X M N O P Q R A B C
U U H H EEEEE FFFFF I J O OA A M M
U U H H E F I J OO OO A A MM MM
U U HHHHH EEEE FFFFF I J O OAAAAA M M M
U UH H E F I J OO OO A A M M
UUUU H H EEEEE F I J O OA A M M
Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.
This Exercise in C# involves creating a program that asks the user for a text string and displays a right-aligned triangle using that string. The triang...
This Exercise in C# involves creating a program that asks the user for a text string and performs three specific transformations on it. The program must...
This Exercise in C# involves creating a struct to store two pieces of data for a person: their name and their date of birth. The date of birth...
This Exercise in C# involves creating a program that asks the user for 10 integer numbers (from -1000 to 1000), sorts them, and then displays them in ascending...
This Exercise in C# involves creating a program that declares a 70x20 two-dimensional array of characters and "draws" 80 letters (e.g., 'X') in random positions withi...
This Exercise in C# involves creating a program that declares a 70x20 two-dimensional array of characters and "draws" a circumference with a radius of 8 inside it. To...