Skip to content

Instantly share code, notes, and snippets.

@ChrisMoney
Created February 15, 2012 12:24
Show Gist options
  • Save ChrisMoney/bec7c1010870853b55f6 to your computer and use it in GitHub Desktop.
Save ChrisMoney/bec7c1010870853b55f6 to your computer and use it in GitHub Desktop.
C# --Code justifies and aligns a set of strings to improve the appearance of program output
/ AlignOutPut – left justify and align a set of strings to improve the appearance of program output
namespace AlignOutPut
{
using System;
class Program
{
public static void Main(string[] args)
{
string[] names = (“Christa”, “Sarah”, “Jonathan”, “Sam”, “Schmekowitz”);
// first output the names as they start out
Console.WriteLine(“The following names are of” + different.lengths”);
foreach(string s in names)
{
console.WriteLine(“This is the name ‘{0}’ before”, s);
}
Console.WriteLine();
// this time, fix the strings so they are left justified and all the same length
string[] sAlignedNames = TrimAndPad(names);
// finally output the resulting padded justified strings
Console.WriteLine(“The following are the same names” + normalized to the same length”)
foreach(string s in sAlignedNames)
{
Console.WriteLine(“This is the name “{0}” afterwards”, s);
}
// wait for user to acknowledge
Console.WriteLine(“Press Enter to terminate…”);
Console.Read();
}
// TrimAndPad – given an array of strings , trim whtespace from both ends and then repad the strings to // align them with the longest member
public static string[] TrimAndPad(string[] strings)
{
// copy the source array into an array that you can manipulate
string[] stringsToAlign = new String(strings.Length);
// first remove any unnecessary spaces from either end of the names
for (int I = 0; I < stringsToAlign.Length; i++)
{
stringsToAlign[i] = strings[i].Trim();
}
// now find the length of the longest string so that all other strings line up with that string
int nMaxLength = 0;
foreach(string s in stringsToAlign)
{
if (s.Length > nMaxLength)
(
nMaxLength = s.Length;
}
}
// finally justify all the strings to the length of the maximum string
for (int I = 0; I < stringsToAlign.Length; i++)
{
stringsToAlign[i] = stringsToAlign[i].PadRight (nMaxLength + 1);
}
// return the result to the caller
return stringsToAlign;
}}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment