• Register
0 votes


Hi there! I am new to the world of programming. My programming teacher gave me an assignment in which I have to write a program in which there would be a function that will take two strings as input from user and will compare the size of both strings and then it will return the larger string of the two. I have no clue how to do that. Please provide me with the sample program with proper description. Thanks 
10 6 3
5,520 points

Please log in or register to answer this question.

1 Answer

0 votes


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:


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);

        //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


Enter First String:
This is the larger string
Enter Second String:
This is smaller
The larger string is: This is the larger string
12 5 3
17,660 points

Related questions

0 votes
2 answers 253 views
Problem: I am new to programming, so need query to get query to return max number from 3 integer? Write the definition of a method max that has three int parameters and returns the largest? Can anyone guide with correct code?
asked Feb 17, 2020 maddi86 5.4k points
0 votes
2 answers 99 views
I'm new in java programming, I know how to find out the largest or smallest number from an array. But I'm facing problem when I'm trying to find out smallest staring in 3 string. Please help me write a method min that has three string parameters and returns the smallest.
asked May 2, 2020 samhaz 5k points
0 votes
1 answer 12 views
Problem: I want to write a function that receives two pointers to functions and returns a pointer to the function composicion. Is it possible to do that? This was my idea: typedef int (* integerFunction) (int); integer function composition (integer function f1, integer function f2) { return f1 (f2); }
asked Jan 1 sasha 6.4k points
0 votes
1 answer 5 views
Problem: Would the following code be correct? class Person { private: string name; bool sex; int age; public: Person (); bool getSexo (); int getAge (); Person play (string s); }Hope someone could help me out?
asked 1 day ago sasha 6.4k points
0 votes
1 answer 99 views
I start programming recently and I love to do programming but every day I'm facing new problems. I wrote a java code where I can easily take two int input from a user and I'm able to print the smallest value. But I'm doing all things in the main method. I don't want to do ... smallest; if(a>b){ smallest = b; }else{ smallest = a; } System.out.println("Smallest is: "+smallest); } Here is my code.
asked May 3, 2020 samhaz 5k points
0 votes
1 answer 122 views
Problems: I need help in programming to solve my issue. Is there anyone who can help me ? Write a complete main method that prints Hello, world to the screen?
asked Feb 13, 2020 maddi86 5.4k points
0 votes
1 answer 66 views
I started programming recently, I have learned some basic programming topics like loops, array and many more. But they are not enough to implement my concept in java. I can write a void method that can take 2 or more parameters and print the summation of all parameters. But now I want to ... * @param a * @param b */ public static void add(int a, int b){ int sum = a+b; System.out.println(sum); }
asked May 3, 2020 samhaz 5k points
0 votes
1 answer 10 views
Problem: I want to implement a kind of console (function menu_principal), where the user can go to different menus according to their choice. One of these options is to enter a directory (function dir_manager, accessed with the number 1). Once the user wants to end the execution, he must type the ... * option1 = 1; // <---- THIS IS THE ERROR return; } else if (* option1 == 9) { return; } } }
asked Jan 1 sasha 6.4k points
0 votes
1 answer 3 views
Problem: I am programming a game in c ++ plus using the SFML library and I want that when the player loses, his name will be saved along with the score obtained. The code I have so far is the following: while (mWindow-> pollEvent (event)) { if (event.type = ... , since before instead of using while, use an if but only saved in the file, the letter of the last key pressed before closing the window.
asked 2 days ago sasha 6.4k points
0 votes
1 answer 90 views
Solutions: I am new in programming and my logic is not good, can anyone give the right solution? Write an expression that evaluates to true if and only if the C-string s equals the C-string "end"?
asked Feb 14, 2020 maddi86 5.4k points