- Course
- Functions A
- Function to write format text
Function to write format text
Last updated:
8/23/2020
⁃
Difficulty:
Intermediate
Create a program in C# that implements a function called WriteCenteredUnderline that receives as a parameter a text requested from the user. Then show the text centered and underlined using the '-' character on the screen, assuming we have 80 spaces wide.
Input
Hello
Output
Hello
-----
Solution
using System;
public class FunctionWriteCenteredUnderlined
{
public static void Main(string[] args)
{
string text = Console.ReadLine();
WriteCenteredUnderlined(text);
}
public static void WriteCenteredUnderlined(string text)
{
int countSpaces = (80 - text.Length) / 2;
int i = 0;
for (; i < countSpaces; i++)
{
Console.Write(" ");
}
Console.WriteLine(text);
for (i = 0; i < countSpaces; i++)
{
Console.Write(" ");
}
for (i = 0; i < text.Length; i++)
{
Console.Write("_");
}
}
}