Check if two Strings are equal without using built-in function – Java

Objective– Given two strings, find out if they are equal or not without using any built-in function (without using equals() function in java).

Example:

String x='tutorial' and String y='tutorial' are equal - true
String x='tutorial' and String y='tutorial ' are equal - false
String x='tutorial' and String y=' ' are equal – false

Approach:

  • If any of the string is null, return false.
  • If lengths of both strings are not matching, return false.
  • Check if all the characters of both strings are matching, if not return false.
  • If all the steps above got executed without returning false then return true.

Code:



Output:

String x='tutorial' and String y='tutorial' are equal?? -true
String x='tutorial' and String y='tutorial ' are equal?? -false
String x='tutorial' and String y=' ' are equal?? -false

__________________________________________________
Top Companies Interview Questions..-

Google Microsoft Amazon Facebook more..

If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.
__________________________________________________

%d bloggers like this: