Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C# Sharp Exercises: Perform an ordinal comparison of two strings that only differ in case

C# Sharp String: Exercise-31 with Solution

Write a C# Sharp program to perform and ordinal comparison of two strings that only differ in case.

Sample Solution:-

C# Sharp Code:

using System;
class Example31
{
    public static void Main() {
    String str1 = "JAVA";
    String str2 = "python";
    String str;
    int result;

    Console.WriteLine();
    Console.WriteLine("Compare the numeric values of the corresponding Char objects in each string.");
    Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2);
    result = String.CompareOrdinal(str1, str2);
    str = ((result < 0) ? "less than" : ((result > 0) ? "greater than" : "equal to"));
    Console.Write("String '{0}' is ", str1);
    Console.Write("{0} ", str);
    Console.WriteLine("String '{0}'.", str2);
    }
}

Sample Output:

Compare the numeric values of the corresponding Char objects in each string.                                  
str1 = 'JAVA', str2 = 'python'                                                                                
String 'JAVA' is less than String 'python'.

Flowchart :

Flowchart: C# Sharp Exercises - Performs and ordinal comparison of two strings that only differ in case.

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a C# Sharp program to demonstrate that CompareOrdinal and Compare use different sort orders.
Next: Write a C# Sharp program to compare a given string with set of strings.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.