nKolchakoV

CompareChars

Dec 7th, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.78 KB | None | 0 0
  1. using System;
  2. //Write a program that compares two char arrays lexicographically (letter by letter).
  3.  
  4. class Program
  5. {
  6. static void Main()
  7. {
  8.     Console.Write("Enter the length of 1st array: ");
  9.     int lengthOne = int.Parse(Console.ReadLine());
  10.     char[] arrayOne = new char[lengthOne];
  11.     for (int firstArray = 0; firstArray < lengthOne; firstArray++)
  12.     {
  13.         Console.Write("Enter array[{0}] = ", firstArray);
  14.         arrayOne[firstArray] = char.Parse(Console.ReadLine());
  15.     }
  16.     Console.Write("Enter the length of 2nd array: ");
  17.     int lengthTwo = int.Parse(Console.ReadLine());
  18.     char[] arrayTwo = new char[lengthTwo];
  19.     for (int secondArray = 0; secondArray < lengthTwo; secondArray++)
  20.     {
  21.         Console.Write("Enter array[{0}] = ", secondArray);
  22.         arrayTwo[secondArray] = char.Parse(Console.ReadLine());
  23.     }
  24.     //start Compare
  25.     bool isEqual = false;
  26.     if (lengthOne == lengthTwo)
  27.     {
  28.         for (int i = 0; i < lengthOne; i++)
  29.         {
  30.             if (arrayOne[i] == arrayTwo[i])
  31.             {
  32.                 isEqual = true;
  33.             }
  34.             if (arrayOne[i] < arrayTwo[i])
  35.             {
  36.                 Console.WriteLine("Array one is lexicographically first");
  37.                 break;
  38.             }
  39.             if (arrayTwo[i] < arrayOne[i])
  40.             {
  41.                 Console.WriteLine("Array two is lexicographically first");
  42.                 break;
  43.             }
  44.         }
  45.     }
  46.     else if (lengthOne > lengthTwo)
  47.     {
  48.         Console.WriteLine("Array two is lexicographically first");
  49.     }
  50.     else if (lengthTwo > lengthOne)
  51.     {
  52.         Console.WriteLine("Array one is lexicographically first");
  53.     }
  54.     if (isEqual == true)
  55.     {
  56.         Console.WriteLine("The two arrays are Equal !");
  57.     }
  58.  
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment