Reads in 3 user specified strings and sorts them in alphabetic order. - CSharp Language Basics

CSharp examples for Language Basics:string

Description

Reads in 3 user specified strings and sorts them in alphabetic order.

Demo Code

using System;//from   ww w  . j  a  va 2 s.co  m
class StringSorter
{
   public static void Main()
   {
      string string1;
      string string2;
      string string3;
      string tempString;
      bool changes = true;
      Console.Write("Enter first string: ");
      string1 = Console.ReadLine();
      Console.Write("Enter second string: ");
      string2 = Console.ReadLine();
      Console.Write("Enter third string: ");
      string3 = Console.ReadLine();
      while(changes)
      {
         changes = false;
         if(string1.CompareTo(string2) > 0)
         {
            tempString = string1;
            string1 = string2;
            string2 = tempString;
         }
         if(string2.CompareTo(string3) > 0)
         {
            tempString = string2;
            string2 = string3;
            string3 = tempString;
            changes = true;
         }
      }
      Console.WriteLine("The strings in alphabetic order:");
      Console.WriteLine(string1);
      Console.WriteLine(string2);
      Console.WriteLine(string3);
   }
}

Result


Related Tutorials