Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class CharArraysWhoIsEarlier
- {
- static void Main()
- {
- Console.WriteLine("Enter first text");
- string firstText = Console.ReadLine(); //Read first text into string
- char[] firstArray = new char[firstText.Length]; //Set array's lenght == string chars
- for (int i = 0; i < firstText.Length; i++)
- {
- firstArray[i] = firstText[i]; //Put chars into array
- }
- Console.WriteLine("Enter second text");
- string secondText = Console.ReadLine(); //Read second text into string
- char[] secondArray = new char[secondText.Length];
- if (firstText.Length == secondText.Length) //if arrays' lenght is equal continue to compare chars
- {
- for (int i = 0; i < secondText.Length; i++)
- {
- secondArray[i] = secondText[i]; //Put chars into array
- if (firstArray[i] < secondArray[i]) //If first array char is lower value in ascii the array is earlier
- {
- Console.WriteLine("First text is earlier");
- break;
- }
- else if (firstArray[i] > secondArray[i]) //The opposite case, second array is earlier
- {
- Console.WriteLine("Second text is earlier");
- break;
- }
- else if (firstArray[firstArray.Length - 1] == secondArray[secondArray.Length - 1])
- {
- Console.WriteLine("Arrays are equal"); //Arrays are equal if all chars are equal
- }
- }
- } //The array with less cahrs is earlier
- else if (firstText.Length < secondText.Length)
- {
- Console.WriteLine("First text is earlier");
- }
- else
- {
- Console.WriteLine("Second text is earlier");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement