Advertisement
Klaxon

[C# Arrays] Char Array Comparison

Jul 15th, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.23 KB | None | 0 0
  1. // 3. Write a program that compares two char arrays lexicographically (letter by letter).
  2.  
  3. using System;
  4.  
  5. class CharArrayComparison
  6. {
  7.     static void Main()
  8.     {
  9.         // Get the array size
  10.         Console.Write("Size of the arrays: ");
  11.         int length = int.Parse(Console.ReadLine());
  12.  
  13.         char[] firstArray = new char[length];
  14.         char[] secondArray = new char[length];
  15.  
  16.         bool equal = true;
  17.  
  18.         for (int i = 0; i < length; i++)
  19.         {
  20.             // Read symbols for each array
  21.             Console.Write("firstArray[{0}]: ", i);
  22.             firstArray[i] = char.Parse(Console.ReadLine());
  23.             Console.Write("secondArray[{0}]: ", i);
  24.             secondArray[i] = char.Parse(Console.ReadLine());
  25.  
  26.             // Comparing the arrays and printing the result
  27.             if (firstArray[i] == secondArray[i])
  28.             {
  29.                 Console.WriteLine("firstArray[{0}] == secondArray[{1}]", i, i);
  30.             }
  31.             else
  32.             {
  33.                 Console.WriteLine("firstArray[{0}] != secondArray[{1}]", i, i);
  34.                 equal = false;
  35.             }
  36.             Console.WriteLine();
  37.         }
  38.         Console.WriteLine("Is the arrays equal: {0}", equal);
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement