Advertisement
Booster

Char Arrays Find Who Is Earlier

Jul 30th, 2014
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.92 KB | None | 0 0
  1. using System;
  2.  
  3. class CharArraysWhoIsEarlier
  4. {
  5.     static void Main()
  6.     {
  7.         Console.WriteLine("Enter first text");
  8.         string firstText = Console.ReadLine();      //Read first text into string
  9.         char[] firstArray = new char[firstText.Length]; //Set array's lenght == string chars
  10.      
  11.         for (int i = 0; i < firstText.Length; i++)
  12.         {
  13.             firstArray[i] = firstText[i];       //Put chars into array
  14.         }
  15.         Console.WriteLine("Enter second text");
  16.         string secondText = Console.ReadLine(); //Read second text into string
  17.         char[] secondArray = new char[secondText.Length];
  18.    
  19.         if (firstText.Length == secondText.Length)    //if arrays' lenght is equal continue to compare chars
  20.         {
  21.             for (int i = 0; i < secondText.Length; i++)
  22.             {
  23.                 secondArray[i] = secondText[i];         //Put chars into array
  24.                 if (firstArray[i] < secondArray[i])     //If first array char is lower value in ascii the array is earlier
  25.                 {
  26.                     Console.WriteLine("First text is earlier");
  27.                     break;
  28.                 }
  29.                 else if (firstArray[i] > secondArray[i])  //The opposite case, second array is earlier
  30.                 {
  31.                     Console.WriteLine("Second text is earlier");
  32.                     break;
  33.                 }
  34.                 else if (firstArray[firstArray.Length - 1] == secondArray[secondArray.Length - 1])
  35.                 {
  36.                     Console.WriteLine("Arrays are equal"); //Arrays are equal if all chars are equal
  37.                 }
  38.             }
  39.         }       //The array with less cahrs is earlier
  40.         else if (firstText.Length < secondText.Length)
  41.         {
  42.             Console.WriteLine("First text is earlier");
  43.         }
  44.         else
  45.         {
  46.             Console.WriteLine("Second text is earlier");
  47.         }
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement