Advertisement
dimov

Untitled

Jan 12th, 2013
455
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.76 KB | None | 0 0
  1. /* Write a program that compares two char arrays lexicographically (letter by letter). */
  2.  
  3. using System;
  4.  
  5. class CharArraysCompare
  6. {
  7.     static void Main()
  8.     {
  9.         Console.WriteLine("Please enter length for the first array");
  10.         int n = int.Parse(Console.ReadLine());
  11.         Console.WriteLine("Please enter length for the second array");
  12.         int p = int.Parse(Console.ReadLine());
  13.         Console.WriteLine("Please enter the first {0} characters for the first string", n);
  14.         char[] array1 = new char[n];
  15.         Console.WriteLine("Please enter the first {0} characters for the second string", p);
  16.         char[] array2 = new char[p];
  17.         bool same = true;
  18.  
  19.         for (int i = 0; i < array1.Length; i++)
  20.         {
  21.             array1[i] = char.Parse(Console.ReadLine());
  22.         }
  23.  
  24.         for (int i = 0; i < array2.Length; i++)
  25.         {
  26.             array2[i] = char.Parse(Console.ReadLine());
  27.         }
  28.  
  29.         if (array1.Length>array2.Length)
  30.         {
  31.             Console.WriteLine("lexicographically the second string is shorter");
  32.         }
  33.         else
  34.         {
  35.             if (array1.Length < array2.Length)
  36.             {
  37.                 Console.WriteLine("lexicographically the first string is shorter");
  38.             }
  39.             else
  40.             {
  41.                 for (int i = 0; i < array1.Length; i++)
  42.                 {
  43.                     if (array1[i] != array2[i])
  44.                     {
  45.                         same = false;
  46.                     }
  47.                    
  48.                 }
  49.                 if (same == true)
  50.                 {
  51.                     Console.WriteLine("The two arrays are  lexicographically the same?");
  52.                 }
  53.             }
  54.         }
  55.        
  56.  
  57.        
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement