Home
C# ZigZag Pattern From String
class Program
{
public static string[,] grid = new string[1, 1];
public static int myDowns = 0;
public static int myAcross = 0;
static void Main()
{
Console.WriteLine("Enter String To ZigZag");
string myInput = Console.ReadLine();
Console.WriteLine("Enter The Number of \"Downs\"");
myDowns = Convert.ToInt32(Console.ReadLine());
double accMath = myInput.Length / 2;
myAcross = Convert.ToInt32(Math.Ceiling(accMath));
grid = new string[myAcross, myDowns];
MakeGrid();
CalcGrid();
InsertString(myInput);
ReadGrid();
ShowGrid();
}
static void ReadGrid()
{
string qwe = "";
for (int y = 0; y < myDowns; y++)
{
for (int x = 0; x < myAcross; x++)
{
if (grid[x, y] != " " && grid[x, y] != "~")
{
qwe += grid[x, y];
}
}
}
Console.WriteLine("The Answer is ...");
Console.WriteLine(qwe);
Console.WriteLine("");
}
static void InsertString(string qwe)
{
int counter = 0;
for (int x = 0; x < myAcross; x++)
{
for (int y = 0; y < myDowns; y++)
{
if (grid[x, y] == "~" && counter < qwe.Length)
{
grid[x, y] = Convert.ToString(qwe[counter]);
counter++;
}
}
}
}
static void CalcGrid()
{
for (int y = 0; y < myDowns; y++)
{
for (int x = 0; x < myAcross; x++)
{
if(IsDivisible(x) || IsDivisible(x + y))
{
grid[x, y] = "~";
}
}
}
}
static bool IsDivisible(int x)
{
if(x % (myDowns - 1) == 0)
{
return true;
}
return false;
}
static void MakeGrid()
{
for(int y = 0; y < myDowns; y++)
{
for(int x = 0; x < myAcross; x++)
{
grid[x, y] = " ";
}
}
}
static void ShowGrid()
{
for (int y = 0; y < myDowns; y++)
{
for (int x = 0; x < myAcross; x++)
{
Console.Write(grid[x, y]);
}
Console.WriteLine();
}
}
}
Reader's Comments
Name
Comment
Add a RELEVANT link (not required)
Upload an image (not required)
Uploading...
Home