Check if two strings are same ignoring their cases | Function to compare two strings ignoring their cases ; length of first string ; length of second string ; if length is not same simply return false since both string can not be same if length is not equal ; loop to match one by one all characters of both string ; if current characters of both string are same , increase value of i to compare next character ; if any character of first string is some special character or numeric character and not same as corresponding character of second string then return false ; do the same for second string ; this block of code will be executed if characters of both strings are of different cases ; compare characters by ASCII value ; if characters matched , increase the value of i to compare next char ; if all characters of the first string are matched with corresponding characters of the second string , then return true ; Function to print the same or not same if strings are equal or not equal ; Driver Code
function equalIgnoreCase ( str1 , str2 ) { let i = 0 ; let len1 = str1 . length ; let len2 = str2 . length ; if ( len1 != len2 ) return false ; while ( i < len1 ) { if ( str1 [ i ] == str2 [ i ] ) { i ++ ; } else if ( ! ( ( str1 [ i ] . charCodeAt ( ) >= ' ' . charCodeAt ( ) && str1 [ i ] . charCodeAt ( ) <= ' ' . charCodeAt ( ) ) || ( str1 [ i ] . charCodeAt ( ) >= ' ' . charCodeAt ( ) && str1 [ i ] . charCodeAt ( ) <= ' ' . charCodeAt ( ) ) ) ) { return false ; } else if ( ! ( ( str2 [ i ] . charCodeAt ( ) >= ' ' . charCodeAt ( ) && str2 [ i ] . charCodeAt ( ) <= ' ' . charCodeAt ( ) ) || ( str2 [ i ] . charCodeAt ( ) >= ' ' . charCodeAt ( ) && str2 [ i ] . charCodeAt ( ) <= ' ' . charCodeAt ( ) ) ) ) { return false ; } else { if ( str1 [ i ] . charCodeAt ( ) >= ' ' . charCodeAt ( ) && str1 [ i ] . charCodeAt ( ) <= ' ' . charCodeAt ( ) ) { if ( str1 [ i ] . charCodeAt ( ) - 32 != str2 [ i ] . charCodeAt ( ) ) return false ; } else if ( str1 [ i ] . charCodeAt ( ) >= ' ' . charCodeAt ( ) && str1 [ i ] . charCodeAt ( ) <= ' ' . charCodeAt ( ) ) { if ( str1 [ i ] . charCodeAt ( ) + 32 != str2 [ i ] . charCodeAt ( ) ) return false ; } i ++ ; return true ; function equalIgnoreCaseUtil ( str1 , str2 ) { let res = equalIgnoreCase ( str1 , str2 ) ; if ( res == true ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ; } let str1 , str2 ; str1 = " " ; str2 = " " ; equalIgnoreCaseUtil ( str1 , str2 ) ; str1 = " " ; str2 = " " ; equalIgnoreCaseUtil ( str1 , str2 ) ;