java program to compare two strings
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
//taking input of 2 Strings from the user to check
String s=sc.next();
String t=sc.next();
//calling the userdefined function compare and saving the result in a
//boolean variable
boolean b=compare(s,t);
//if the returned result is true,it will print "same" else "not same"
if(b==true)
{
System.out.println("Same");
}
else
{
System.out.println("not same");
}
} // main method end
//compare function
public boolean compare(String a,String b)
{
int count=0;
if(a.length()==b.length())
{
for(int i=0;i<a.length();i++)
{
if(a.charAt(i)==b.charAt(i))
{
count ++;
} //inner if1 end
} //for end
if(count==a.length())
{
return true;
}//inner if 2 end
else
{
return false;
} //inner else end
} //outer if end
else
{
return false;
} //outer else end
} //compare function end
} //class end
// Method call convention
==========================================================
1.If the method is in the same class and static we can call the method directly by its name, but if it is not static we have to call it by its class name.
2.If method is in a different package, then we have to import that package to use that method.
3.Methos is in a different class, then create the object of that class and by using the object reference we can call that method.
#without using compareTo()
#without using compareTo method
#withoutUsingCompareToMethod
#Compare two strings
#compareTwoStringsUsingJavaStringProgramming
#java Programming
#java Programs
#java Program
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
//taking input of 2 Strings from the user to check
String s=sc.next();
String t=sc.next();
//calling the userdefined function compare and saving the result in a
//boolean variable
boolean b=compare(s,t);
//if the returned result is true,it will print "same" else "not same"
if(b==true)
{
System.out.println("Same");
}
else
{
System.out.println("not same");
}
} // main method end
//compare function
public boolean compare(String a,String b)
{
int count=0;
if(a.length()==b.length())
{
for(int i=0;i<a.length();i++)
{
if(a.charAt(i)==b.charAt(i))
{
count ++;
} //inner if1 end
} //for end
if(count==a.length())
{
return true;
}//inner if 2 end
else
{
return false;
} //inner else end
} //outer if end
else
{
return false;
} //outer else end
} //compare function end
} //class end
// Method call convention
==========================================================
1.If the method is in the same class and static we can call the method directly by its name, but if it is not static we have to call it by its class name.
2.If method is in a different package, then we have to import that package to use that method.
3.Methos is in a different class, then create the object of that class and by using the object reference we can call that method.
#without using compareTo()
#without using compareTo method
#withoutUsingCompareToMethod
#Compare two strings
#compareTwoStringsUsingJavaStringProgramming
#java Programming
#java Programs
#java Program
Comments
Post a Comment