Solution:
You can use Length property of strings to compare the size of two or more strings. Following is the brief description of Length property:
Length Property:
In C#, there is a Length property for every string, array, list or collection in general, that is used to check the number of characters, elements and items in collections, respectively. The syntax for using Length property is as follows:
collection_name.Length;
Following is the sample program that you asked for:
using System;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
//Declare two string variables to take input from user
string str1, str2;
//Ask user to enter first string
Console.WriteLine("Enter First String: ");
str1 = Console.ReadLine();
//Ask user to enter second string
Console.WriteLine("Enter Second String: ");
str2 = Console.ReadLine();
//Another string that catches the value returned by the function.
//The strings entered by the user are passed as arguments to the Max() function.
string output = Max(str1, str2);
//printing the result
Console.WriteLine("The larger string is: " + output);
Console.ReadKey();
}
//Definition of the function that compares two strings and returns the larger one
static string Max(string s1, string s2)
{
//comparing two strings
if (s1.Length > s2.Length) //if true
return s1; //executed
else if (s2.Length > s1.Length) //if true
return s2; //executed
else //if both of the above conditions are false
return "Both strings are equal in size"; //executed
}
}
}
Output:
Enter First String:
This is the larger string
Enter Second String:
This is smaller
The larger string is: This is the larger string