Beginners Programs :

Java String Case Insensitive Comparison | Java Programs

Java string case insensitive program. In the Java case insensitive comparison program, there will be suitable examples as well as sample output for your better understanding.

This code is for checking for equality of two user-defined strings irrespective of their case (i.e. uppercase or lowercase) using Java language.

  • The problem here is to check whether two user-defined strings are equal or not irrespective of their case (example : Hello is equal to hello).
  • Our constraint here is getting incorrect outputs for certain characters which are not the uppercase or lowercase of a given character but have a difference of 32 in ASCII values (example: A=65 and !=33).
  • The input here is two user-defined strings in any case i.e. uppercase or lowercase.
  • The output is a boolean value of true or false signifying whether the two strings are equal or not.

Output – 1:

  • Solution :
  1. In the first line, a new Scanner class object is initialized and a reference variable sc is set to represent the object. This class is used to take user input in Java case insensitive comparison.
  2. Scanner class is part of the java.util package and hence the import statement, in the beginning, is stated to import the functionality of the specified class.
  3. The next line prints a statement to the console instructing the user to enter a string.
  4. The .nextLine() method is called by the Scanner object sc. This method reads a line of user input of a string value and stores it to the variable s1.
  5. This same process is repeated again to store the second string in variable s2.
  6. Within, the print statement of the last line, the method Check is called and s1 and s2 are passed in as arguments.
  7. In this method, the first line implements a conditional statement that checks whether the parameters s1 and s2 are equal in length.
  8. The program continues implementing the code if this condition is satisfied but ends its implementation by returning a boolean value of false otherwise.
  9. If the conditional statement is satisfied i.e. the two strings are equal in length, char variables a,b are declared and an int variable is initialized to 0. and will be used to store the character at a certain index position in the string.
  10. for loop iterates over the length of the string (any of the two string lengths can be used as the conditional in the loop since they are both equal in length).
  11. char a is initialized to the character at the index position i in s1 for each iteration of the loop and is initialized to position of s2.
  12. The if condition checks if and for each iteration are equal OR is equal to the lowercase value of char b (there is a difference of 32 in ASCII values of all uppercase and lowercase characters) OR is equal to the uppercase value of b. The ||  operator is used to imply the OR conditional.
  13. For every iteration where the if condition is satisfied, is incremented by 1 using the post-increment operator (++).
  14. If is incremented for all the iterations, it signifies that the if condition was satisfied for all the iterations, the two given strings must be equal irrespective of the case.
  15. Therefore, if is equal to the string length s1, a boolean value of true is returned and printed out and if it is not equal to the length, a value of false is returned and printed.
  • Here, the first string hello is being compared to HELLo. Within the method Check, the variable will be incremented in all the iterations since all the characters are equal when their case is not taken into consideration.

(example : h in hello has an ASCII value of 104. While being compared to H in HELLo, 104-32=72 which is the ASCII value of H satisfies the condition in the loop).

Output – 2:

  • Here, the first string motive is being compared to emotive. The lengths of the two strings are unequal and hence the method Check returns a boolean false, for the Java comparison case-sensitive program.