Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- //Task3:
- //Write a program that compares two char arrays lexicographically (letter by letter).
- namespace Task3CharArrays
- {
- class CharArrays
- {
- static void Main(string[] args)
- {
- try
- {
- Console.Write("Enter the lenght of first array: ");
- int n = int.Parse(Console.ReadLine());
- Console.Write("Enter the lenght of second array: ");
- int m = int.Parse(Console.ReadLine());
- char[] array1 = new char[n];
- char[] array2 = new char[m];
- Console.WriteLine("Enter the letters of first array: ");
- for (int elem1 = 0; elem1 < n; elem1++) //enter first array
- {
- Console.Write("array1[{0}]= ", elem1);
- array1[elem1] = char.Parse(Console.ReadLine());
- }
- Console.WriteLine("Enter the letters of second array: ");
- for (int elem2 = 0; elem2 < m; elem2++) //enter second array
- {
- Console.Write("array2[{0}]= ", elem2);
- array2[elem2] = char.Parse(Console.ReadLine());
- }
- int length = Math.Min(m, n);
- char[] first = new char[length]; //define first and second arrays for sort
- char[] second = new char[length];
- bool equal = false;
- do
- {
- for (int i = 0; i < length; i++)
- {
- if ((int)array1[i] == (int)array2[i]) //compere the elements of arrays
- {
- equal = true;
- if ((i == length - 1) && (equal == true))
- {
- equal = false;
- if (m == n)
- {
- Console.WriteLine("Arrays are equal"); //all compeared element are equal
- }
- else if (m > n) //all compeared elements are equal but one array is shorter
- {
- first = array1;
- second = array2;
- }
- else if (m < n)
- {
- first = array2;
- second = array1;
- }
- }
- }
- else if (((int)array1[i] < (int)array2[i]))
- {
- first = array1;
- second = array2;
- equal = false;
- break;
- }
- else if (((int)array1[i] > (int)array2[i]))
- {
- first = array2;
- second = array1;
- equal = false;
- break;
- }
- }
- }
- while (equal == true);
- Console.WriteLine("Lexicographically Sorted:"); //sorted arrays lexicographically
- Console.Write("1. ");
- foreach (char element in first)
- {
- Console.Write(element);
- }
- Console.WriteLine();
- Console.Write("2. ");
- foreach (char element in second)
- {
- Console.Write(element);
- }
- Console.WriteLine();
- }
- catch
- {
- Console.WriteLine("Enter incorrect data!");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement