Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- //Write a program that compares two char arrays lexicographically (letter by letter).
- class Program
- {
- static void Main()
- {
- Console.Write("Enter the length of 1st array: ");
- int lengthOne = int.Parse(Console.ReadLine());
- char[] arrayOne = new char[lengthOne];
- for (int firstArray = 0; firstArray < lengthOne; firstArray++)
- {
- Console.Write("Enter array[{0}] = ", firstArray);
- arrayOne[firstArray] = char.Parse(Console.ReadLine());
- }
- Console.Write("Enter the length of 2nd array: ");
- int lengthTwo = int.Parse(Console.ReadLine());
- char[] arrayTwo = new char[lengthTwo];
- for (int secondArray = 0; secondArray < lengthTwo; secondArray++)
- {
- Console.Write("Enter array[{0}] = ", secondArray);
- arrayTwo[secondArray] = char.Parse(Console.ReadLine());
- }
- //start Compare
- bool isEqual = false;
- if (lengthOne == lengthTwo)
- {
- for (int i = 0; i < lengthOne; i++)
- {
- if (arrayOne[i] == arrayTwo[i])
- {
- isEqual = true;
- }
- if (arrayOne[i] < arrayTwo[i])
- {
- Console.WriteLine("Array one is lexicographically first");
- break;
- }
- if (arrayTwo[i] < arrayOne[i])
- {
- Console.WriteLine("Array two is lexicographically first");
- break;
- }
- }
- }
- else if (lengthOne > lengthTwo)
- {
- Console.WriteLine("Array two is lexicographically first");
- }
- else if (lengthTwo > lengthOne)
- {
- Console.WriteLine("Array one is lexicographically first");
- }
- if (isEqual == true)
- {
- Console.WriteLine("The two arrays are Equal !");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment