Advertisement
Guest User

[Exam Problems] C# Basics Problem{4} Longest Alphabetical Wo

a guest
Dec 5th, 2014
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.95 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3.  
  4. class LongestAlphabeticalWord
  5. {
  6.     static void Main()
  7.     {
  8.         string word = Console.ReadLine();
  9.         int n = int.Parse(Console.ReadLine());
  10.         char[,] array = new char[n, n];
  11.         //fill array with the letters of the word
  12.         int i = 0;
  13.         for (int row = 0; row < array.GetLength(0); row++)
  14.         {
  15.             for (int col = 0; col < array.GetLength(1); col++)
  16.             {
  17.                 if (i < word.Length)
  18.                 {
  19.                     array[row, col] = word[i];
  20.                 }
  21.                 else
  22.                 {
  23.                     i = 0;
  24.                     array[row, col] = word[i];
  25.                 }
  26.                 i++;
  27.             }
  28.         }
  29.         //print array to test
  30.         for (int row = 0; row < array.GetLength(0); row++)
  31.         {
  32.             for (int col = 0; col < array.GetLength(1); col++)
  33.             {
  34.                 //Console.Write("{0} ", array[row, col]);
  35.             }
  36.             //Console.WriteLine();
  37.         }
  38.         //if the array size is 1:
  39.         if (n == 1)
  40.         {
  41.             Console.WriteLine(array[0, 0]);
  42.             return;
  43.         }
  44.         //set up variables
  45.         StringBuilder currentBuild = new StringBuilder();
  46.         string longest = string.Empty;
  47.         //check rows from left to right
  48.         for (int row = 0; row < array.GetLength(0); row++)
  49.         {
  50.             for (int col = 0; col < array.GetLength(1) - 1; col++)
  51.             {
  52.                 if (col == 0)
  53.                 {
  54.                     currentBuild.Append(array[row, col]);
  55.                 }
  56.                 if (array[row, col] < array[row, col + 1])
  57.                 {
  58.                     currentBuild.Append(array[row, col + 1]);
  59.                 }
  60.                 else
  61.                 {
  62.                     currentBuild.Clear();
  63.                     currentBuild.Append(array[row, col + 1]);
  64.                 }
  65.                 string current = currentBuild.ToString();
  66.                 //use method to check if the new word is the longest
  67.                 longest = ReturnEarlierWord(longest, current);
  68.             }
  69.             currentBuild.Clear();
  70.         }
  71.         //check rows from right to left
  72.         for (int row = 0; row < array.GetLength(0); row++)
  73.         {
  74.             for (int col = array.GetLength(1) - 1; col > 0; col--)
  75.             {
  76.                 if (col == array.GetLength(1) - 1)
  77.                 {
  78.                     currentBuild.Append(array[row, col]);
  79.                 }
  80.                 if (array[row, col] < array[row, col - 1])
  81.                 {
  82.                     currentBuild.Append(array[row, col - 1]);
  83.                 }
  84.                 else
  85.                 {
  86.                     currentBuild.Clear();
  87.                     currentBuild.Append(array[row, col - 1]);
  88.                 }
  89.                 string current = currentBuild.ToString();
  90.                 //use method to check if the new word is the longest
  91.                 longest = ReturnEarlierWord(longest, current);
  92.             }
  93.             currentBuild.Clear();
  94.         }
  95.         //check columns from top to bottom
  96.         for (int col = 0; col < array.GetLength(1); col++)
  97.         {
  98.             for (int row = 0; row < array.GetLength(0) - 1; row++)
  99.             {
  100.                 if (row == 0)
  101.                 {
  102.                     currentBuild.Append(array[row, col]);
  103.                 }
  104.                 if (array[row, col] < array[row + 1, col])
  105.                 {
  106.                     currentBuild.Append(array[row + 1, col]);
  107.                 }
  108.                 else
  109.                 {
  110.                     currentBuild.Clear();
  111.                     currentBuild.Append(array[row + 1, col]);
  112.                 }
  113.                 string current = currentBuild.ToString();
  114.                 //use method to check if the new word is the longest
  115.                 longest = ReturnEarlierWord(longest, current);
  116.             }
  117.             currentBuild.Clear();
  118.         }
  119.         //check columns from bottom to top
  120.         for (int col = 0; col < array.GetLength(1); col++)
  121.         {
  122.             for (int row = array.GetLength(0) - 1; row > 0; row--)
  123.             {
  124.                 if (row == array.GetLength(0) - 1)
  125.                 {
  126.                     currentBuild.Append(array[row, col]);
  127.                 }
  128.                 if (array[row, col] < array[row - 1, col])
  129.                 {
  130.                     currentBuild.Append(array[row - 1, col]);
  131.                 }
  132.                 else
  133.                 {
  134.                     currentBuild.Clear();
  135.                     currentBuild.Append(array[row - 1, col]);
  136.                 }
  137.                 string current = currentBuild.ToString();
  138.                 //use method to check if the new word is the longest
  139.                 longest = ReturnEarlierWord(longest, current);
  140.             }
  141.             currentBuild.Clear();
  142.         }
  143.         //print result!!!
  144.         Console.WriteLine(longest);
  145.     }
  146.     public static string ReturnEarlierWord(string word1, string word2)
  147.     {
  148.         int lengthWord1 = word1.Length;
  149.         int lengthWord2 = word2.Length;
  150.         if (lengthWord1 > lengthWord2)
  151.         {
  152.             return word1;
  153.         }
  154.         else if (lengthWord2 > lengthWord1)
  155.         {
  156.             return word2;
  157.         }
  158.         else if (lengthWord1 == lengthWord2)
  159.         {
  160.             for (int i = 0; i < word1.Length; i++)
  161.             {
  162.                 if (word1[i] < word2[i])
  163.                 {
  164.                     return word1;
  165.                 }
  166.                 else if (word2[i] < word1[i])
  167.                 {
  168.                     return word2;
  169.                 }
  170.             }
  171.         }
  172.         return word1;
  173.     }
  174.     //unused method
  175.     public static int SequenceWeight(string sequence)
  176.     {
  177.         int weight = 0;
  178.         for (int index = 0; index < sequence.Length; index++)
  179.         {
  180.             weight += sequence[index];
  181.         }
  182.         return weight;
  183.     }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement