Advertisement
ntodorova

Arrays - 03_TwoCharsEqual

Jan 12th, 2013
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.92 KB | None | 0 0
  1. using System;
  2.  
  3. /*
  4.  * 3. Write a program that compares two char arrays lexicographically (letter by letter).
  5.  */
  6. class TwoCharsEqual
  7. {
  8.     static void Main()
  9.     {
  10.         bool equal = false;
  11.         bool firstIsBefore = false;
  12.         bool equalDiffSizes = true;
  13.  
  14.         Console.Write("Please enter the first char or string: ");
  15.         string firstStr = Console.ReadLine();
  16.  
  17.         Console.Write("Please enter the second char or string: ");
  18.         string secondStr = Console.ReadLine();
  19.  
  20.         // Used to check whether the algorithm works as the build-in function CompareTo in .NET
  21.         // int resultCompare = firstStr.CompareTo(secondStr);
  22.         // Console.WriteLine(resultCompare);
  23.  
  24.         char[] firstCharArray = firstStr.ToCharArray();
  25.         char[] secondCharArray = secondStr.ToCharArray();
  26.  
  27.         int firstCharArrLength = firstCharArray.Length;
  28.         int secondCharArrLength = secondCharArray.Length;
  29.  
  30.         int minLength = Math.Min(firstCharArrLength, secondCharArrLength);
  31.  
  32.         for (int i = 0; i < minLength; i++)
  33.         {
  34.             if (firstCharArray[i] == secondCharArray[i])
  35.             {
  36.                 equalDiffSizes = true;
  37.             }
  38.             else
  39.             {
  40.                 if (firstCharArray[i] < secondCharArray[i])
  41.                 {
  42.                     firstIsBefore = true;
  43.                 }
  44.                 else
  45.                 {
  46.                     firstIsBefore = false;
  47.                 }
  48.             }
  49.         }
  50.  
  51.         // If all chars are equal till the most right char of the shortest string,
  52.         // then we compare the both string lengths and the one with less chars
  53.         // is lexicographically before the other string.
  54.         if (equalDiffSizes)
  55.         {
  56.             if (firstCharArrLength == secondCharArrLength)
  57.             {
  58.                 equal = true;
  59.             }
  60.             else if (firstCharArrLength < secondCharArrLength)
  61.             {
  62.                 firstIsBefore = true;
  63.             }
  64.         }
  65.  
  66.         if (equal)
  67.         {
  68.             Console.WriteLine("Both strings are equal.");
  69.         }
  70.         else if (firstIsBefore)
  71.         {
  72.             Console.WriteLine("The first string is lexicographically before the second one.");
  73.         }
  74.         else
  75.         {
  76.             Console.WriteLine("The second string is lexicographically before the first one.");
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement