Compare two strings lexicographically

 Compare two strings lexicographically


Problem Description

Write a java program to compare two strings lexicographically

If the strings are equal then mentions as follows:

Input 1:

SRM
SRM

Output:

SRM is equal to SRM
Logic Test Case 1

Input (stdin)
SRM UNIVERSITY

SRM

Expected Output

SRM UNIVERSITY is greater than SRM
Logic Test Case 2

Input (stdin)
SRM

SRM UNIVERSITY

Expected Output

SRM is less than SRM UNIVERSITY




Code Area


import java.io.*;
import java.util.*;
public class TestClass {
public static void main(String[] args) { 
Scanner sc=new Scanner(System.in);
       String s1=sc.nextLine();
       String s2=sc.nextLine();
       if(s1.length()==s2.length())
       {
         System.out.println(s1+" is equal to " +s2);
       }
       else if(s1.length()>s2.length())
       {
         System.out.println(s1+ " is greater than " +s2);
       }
       else
       {
         System.out.println(s1+ " is less than " +s2);
       }
     }
}

Post a Comment

0 Comments