Advertisement
soxa

lexicography

Dec 16th, 2013
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.42 KB | None | 0 0
  1. using System;
  2. //Write a program that compares two char arrays lexicographically (letter by letter).
  3.  
  4. class LexicographicallyCompares
  5. {
  6.     static void Main()
  7.     {
  8.         // Input
  9.  
  10.         string firstString = Console.ReadLine();
  11.         string secondString = Console.ReadLine();
  12.  
  13.         char[] firstArray = firstString.ToCharArray();
  14.         char[] secondArray = secondString.ToCharArray();
  15.  
  16.         int loop = Math.Min(firstArray.Length, secondArray.Length);
  17.         bool check = true;
  18.  
  19.         //Solution
  20.  
  21.         for (int i = 0; i < loop; i++) // Check elements
  22.         {
  23.             if (firstArray[i] > secondArray[i])
  24.             {
  25.                 check = false;
  26.                 Console.WriteLine(secondString);
  27.                 break;
  28.             }
  29.             else if (firstArray[i] < secondArray[i])
  30.             {
  31.                 check = false;
  32.                 Console.WriteLine(firstString);
  33.                 break;
  34.             }
  35.         }
  36.  
  37.         if (check) // If elements are equal, check length
  38.         {
  39.             if (firstArray.Length > secondArray.Length)
  40.             {
  41.                 Console.WriteLine(secondString);
  42.             }
  43.             else if (firstArray.Length < secondArray.Length)
  44.             {
  45.                 Console.WriteLine(firstString);
  46.             }
  47.             else
  48.             {
  49.                 Console.WriteLine("No difference");
  50.             }
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement