Venciity

[C#] BOOK Arrays - 3 Задача

Jan 6th, 2014
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.37 KB | None | 0 0
  1. /* Write a program that compares two char arrays lexicographically (letter by letter). */
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace _3_exercise
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             Console.Write("Please enter the first char elements: ");
  16.             string firstChars = Console.ReadLine();
  17.             char[] firstArray = firstChars.ToCharArray(); //convert string to char array
  18.             int firstArrayLength = firstArray.Length;  //gets length of the first array
  19.  
  20.             Console.Write("Please enter the second char elements: ");
  21.             string secondChars = Console.ReadLine();
  22.             char[] secondArray = secondChars.ToCharArray();
  23.             int secondArrayLength = secondArray.Length;
  24.  
  25.             int minLen = Math.Min(secondArrayLength, firstArrayLength); //gets min length of the two arrays
  26.  
  27.             bool equalCharArrays = true;  // a kind of flag, helps to print the correct output
  28.  
  29.             for (int i = 0; i < minLen; i++)
  30.             {
  31.                 if (firstArray[i] == secondArray[i])
  32.                 {
  33.                     continue;
  34.                 }
  35.                 else
  36.                 {
  37.                     equalCharArrays = false;
  38.                     if (firstArray[i] < secondArray[i])
  39.                     {
  40.                         Console.WriteLine("The first char array is lexicografically before the second.");
  41.                     }
  42.                     else
  43.                     {
  44.                         Console.WriteLine("The second char array is lexicografically before the first.");
  45.                     }
  46.                     break;
  47.                 }
  48.             }
  49.             if (equalCharArrays == true && firstArrayLength < secondArrayLength)
  50.             {
  51.                 Console.WriteLine("The first char array is lexicografically before the second.");
  52.             }
  53.             else if (equalCharArrays == true && firstArrayLength > secondArrayLength)
  54.             {
  55.                 Console.WriteLine("The second char array is lexicografically before the first.");
  56.             }
  57.             else if (equalCharArrays == true && firstArrayLength == secondArrayLength)
  58.             {
  59.                 Console.WriteLine("The arrays are equal.");
  60.             }
  61.         }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment