The source code of java program to compare the lengths of any two strings entered by the user is given below. In this program, firstly user will be asked to enter the first string and after that he/she will be asked to enter the second string in the program. After that, the below java program code will compare the entered two strings and give the result of comparing of program in the output window.
Source Code of Java Program for Comparing the length of two Strings
import java.util.Scanner; class CompareStrings { public static void main(String args[]) { String s1, s2; Scanner in = new Scanner(System.in); System.out.println("Enter the first string"); s1 = in.nextLine(); System.out.println("Enter the second string"); s2 = in.nextLine(); if ( s1.compareTo(s2) > 0 ) System.out.println("First string is greater than second."); else if ( s1.compareTo(s2) < 0 ) System.out.println("First string is smaller than second."); else System.out.println("Both strings are equal."); } }

0 comments:
Post a Comment