Q:

C# program to check given strings are equal or not using equal to (==) operator

0

C# program to check given strings are equal or not using equal to (==) operator

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

C# code for string comparison

Here, we are asking for two strings input from the user and checking them whether they are equal or not using == operator and also ignoring the case.

// C# program to check given strings are equal or not 
// using equal to (==) operator
using System;
using System.IO;
using System.Text;

namespace IncludeHelp
{
    class Test
    {
        // Main Method 
        static void Main(string[] args)
        {
            string str1;
            string str2;

            //input strings
            Console.Write("Enter a string: ");
            str1 = Console.ReadLine();
            Console.Write("Enter another string: ");
            str2 = Console.ReadLine();

            //comparing strings 
            if (str1 == str2)
                Console.WriteLine(""{0}" and "{1}" are equal", str1, str2);
            else
                Console.WriteLine(""{0}" and "{1}" are not equal", str1, str2);

            //another way 
            if ((str1 == str2) == true)
                Console.WriteLine(""{0}" and "{1}" are equal", str1, str2);
            else
                Console.WriteLine(""{0}" and "{1}" are not equal", str1, str2);

            //comparing by ignoring the case 
            //convert both strings in the same case 
            //either in uppercase or lowercase
            Console.WriteLine("By ignoring case...");
            if(str1.ToUpper() == str2.ToUpper())
                Console.WriteLine(""{0}" and "{1}" are equal", str1, str2);
            else
                Console.WriteLine(""{0}" and "{1}" are not equal", str1, str2);

            //hit ENTER to exit the program
            Console.ReadLine();
        }
    }
}

Output

First run:
Enter a string: IncludeHelp
Enter another string: IncludeHelp
"IncludeHelp" and "IncludeHelp" are equal
"IncludeHelp" and "IncludeHelp" are equal
By ignoring case...
"IncludeHelp" and "IncludeHelp" are equal

Second run:
Enter a string: includehelp
Enter another string: INCLUDEHELP
"includehelp" and "INCLUDEHELP" are not equal
"includehelp" and "INCLUDEHELP" are not equal
By ignoring case...
"includehelp" and "INCLUDEHELP" are equal

Third run:
Enter a string: IncludeHelp
Enter another string: IncludeHelp.com
"IncludeHelp" and "IncludeHelp.com" are not equal
"IncludeHelp" and "IncludeHelp.com" are not equal
By ignoring case...
"IncludeHelp" and "IncludeHelp.com" are not equal

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# program to input weekday number and print the w... >>
<< C# program for character comparison...