/*
Do not use '==' operator for comparing strings in java.
Because '==' checks if both objects point to the same memory address or location
and .equals() compares values in the objects.
This program is made using library functions and without using library functions.
Here in this program I am using string library functions .equals() and .equalsIgnoreCase()
which gives output as boolean values true or false.
*/
import java.util.*;
public class str_cmp_cmd_line
{
public static void main(String[] args)
{
System.out.println("First Input String ="+args[0]);
//Commmand line arguments always starts from 0 i.e. args[0]
System.out.println("Second Input String ="+args[1]);
System.out.println("By using string library function of equals()");
System.out.println("Input string1= "+args[0]+" & string2= "+args[1]+" are equal :"+args[0].equals(args[1]));
// Here equals() checks each character with its order and also compares lowercase-uppercase letter
System.out.println("By using string library function of equalsIgnoreCase()");
//which ignores lower or uppercase of letters input and gives boolean value as true or false
System.out.println("Input string1= "+args[0]+" & string2= "+args[1]+" are equal : "+args[0].equalsIgnoreCase(args[1]));
//Without using library functions
int i,flag=1,len1,len2;
len1=args[0].length();
len2=args[1].length();
char str1[] = args[0].toCharArray();
char str2[] = args[1].toCharArray();
if(len1==len2)
{
for(i=0;i<len1;i++)
{
if(str1[i]!=str2[i])
{
flag=0;
break;
}
}
}
else
{
flag=0;
}
if(flag==1)
{
System.out.println("The two string are EQUAL!!!");
System.out.println(args[0]+" = "+args[1]);
}
else
{
System.out.println("The two string are NOT EQUAL!!!");
System.out.println(args[0]+" != "+args[1]);
}
}
}
/*
E:\java_prog>javac str_cmp_cmd_line.java
E:\java_prog>java str_cmp_cmd_line asd Asd
First Input String =asd
Second Input String =Asd
By using string library function of equals()
Input string1= asd & string2= Asd are equal : false
By using string library function of equalsIgnoreCase()
Input string1= asd & string2= Asd are equal : true
The two string are NOT EQUAL!!!
asd != Asd
E:\java_prog>java str_cmp_cmd_line asd asd
First Input String =asd
Second Input String =asd
By using string library function of equals()
Input string1= asd & string2= asd are equal : true
By using string library function of equalsIgnoreCase()
Input string1= asd & string2= asd are equal : true
The two string are EQUAL!!!
asd = asd
*/
// Java Programs | string Compare | String equal |
Comments
Post a Comment
Please do not enter any spam links in the comment box.