Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class CompareTwoCharArraysLetterByLetter
- {
- static void Main()
- {
- //Write a program that compares two char arrays lexicographically (letter by letter).
- //Read the input
- string firstInputChars = Console.ReadLine(),
- secondInputChars = Console.ReadLine();
- //Input the chars from the 2 strings into 2 arrays
- char[] arrayOne = firstInputChars.ToCharArray();
- char[] arrayTwo = secondInputChars.ToCharArray();
- //Find the array lenght for the comparison in case the two arrays have different lenghts we work with the smaller one
- int comparisonLenght = Math.Min(arrayOne.Length, arrayTwo.Length);
- bool areEqualArrays = true;
- for (int i = 0; i < comparisonLenght; i++)
- {
- if (arrayOne[i] != arrayTwo[i])
- {
- areEqualArrays = false;
- if (arrayOne[i] < arrayTwo[i])
- {
- Console.WriteLine("The first array is lexicografically before the second!");
- break;
- }
- else
- {
- Console.WriteLine("The second array is lexicografically before the first!");
- break;
- }
- }
- }
- //Print the result on the console
- if (areEqualArrays == true && arrayOne.Length == arrayTwo.Length)
- {
- Console.WriteLine("The two arrays are equal!");
- }
- else if (areEqualArrays == true && arrayOne.Length < arrayTwo.Length)
- {
- Console.WriteLine("The first array is lexicografically before the second because it is smaller in size!");
- }
- else if (areEqualArrays == true && arrayOne.Length > arrayTwo.Length)
- {
- Console.WriteLine("The second array is lexicografically before the first because it is smaller in size!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement