Advertisement
TanyaPetkova

C# Part II Arrays Exercise 3

Dec 20th, 2013
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.70 KB | None | 0 0
  1. //Write a program that compares two char arrays lexicographically (letter by letter).
  2.  
  3. using System;
  4.  
  5. class CharArrays
  6. {
  7.     static void Main()
  8.     {
  9.         Console.Write("Enter the first word to compare: ");
  10.         char[] firstArray = Console.ReadLine().ToLower().ToCharArray();
  11.  
  12.         Console.Write("Enter the second word to compare: ");
  13.         char[] secondArray = Console.ReadLine().ToLower().ToCharArray();
  14.  
  15.         bool areEqual = true;
  16.  
  17.         for (int i = 0; i < Math.Min(firstArray.Length, secondArray.Length); i++)
  18.         {
  19.             if (firstArray[i] < secondArray[i])
  20.             {
  21.                 Console.WriteLine("'{0}' comes earlier lexicographically, than '{1}'.", new string(firstArray), new string(secondArray));
  22.                 areEqual = false;
  23.                 break;
  24.             }
  25.  
  26.             if (firstArray[i] > secondArray[i])
  27.             {
  28.                 Console.WriteLine("'{0}' comes earlier lexicographically, than '{1}'.", new string(secondArray), new string(firstArray));
  29.                 areEqual = false;
  30.                 break;
  31.             }
  32.         }
  33.  
  34.         if (areEqual && firstArray.Length == secondArray.Length)
  35.         {
  36.             Console.WriteLine("The two arrays are equal.");
  37.         }
  38.         else if (areEqual && firstArray.Length < secondArray.Length)
  39.         {
  40.             Console.WriteLine("'{0}' comes earlier lexicographically, than '{1}'.", new string(firstArray), new string(secondArray));
  41.         }
  42.         else if (areEqual && firstArray.Length > secondArray.Length)
  43.         {
  44.             Console.WriteLine("'{0}' comes earlier lexicographically, than '{1}'.", new string(secondArray), new string(firstArray));
  45.         }
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement