Compare two strings considering only alphanumeric characters | Function to check alphanumeric equality of both strings ; variable declaration ; Length of first string ; Length of second string ; To check each and every characters of both string ; If the current character of the first string is not an alphanumeric character , increase the pointer i ; If the current character of the second string is not an alphanumeric character , increase the pointer j ; if all alphanumeric characters of both strings are same then return true ; if any alphanumeric characters of both strings are not same then return false ; If current character matched , increase both pointers to check the next character ; If not same , then return false ; Function to print Equal or Unequal if strings are same or not ; check alphanumeric equality of both strings ; if both are alphanumeric equal , print Equal ; otherwise print Unequal ; Driver code
function CompareAlphanumeric ( str1 , str2 ) { let i , j ; i = 0 ; j = 0 ; let len1 = str1 . length ; let len2 = str2 . length ; while ( i <= len1 && j <= len2 ) { while ( i < len1 && ( ! ( ( str1 [ i ] . charCodeAt ( ) >= ' ' . charCodeAt ( ) && str1 [ i ] . charCodeAt ( ) <= ' ' . charCodeAt ( ) ) || ( str1 [ i ] . charCodeAt ( ) >= ' ' . charCodeAt ( ) && str1 [ i ] . charCodeAt ( ) <= ' ' . charCodeAt ( ) ) || ( str1 [ i ] . charCodeAt ( ) >= ' ' . charCodeAt ( ) && str1 [ i ] . charCodeAt ( ) <= ' ' . charCodeAt ( ) ) ) ) ) { i ++ ; } while ( j < len2 && ( ! ( ( str2 [ j ] . charCodeAt ( ) >= ' ' . charCodeAt ( ) && str2 [ j ] . charCodeAt ( ) <= ' ' . charCodeAt ( ) ) || ( str2 [ j ] . charCodeAt ( ) >= ' ' . charCodeAt ( ) && str2 [ j ] . charCodeAt ( ) <= ' ' . charCodeAt ( ) ) || ( str2 [ j ] . charCodeAt ( ) >= ' ' . charCodeAt ( ) && str2 [ j ] . charCodeAt ( ) <= ' ' . charCodeAt ( ) ) ) ) ) { j ++ ; } if ( i == len1 && j == len2 ) { return true ; } else if ( str1 [ i ] != str2 [ j ] ) { return false ; } else { i ++ ; j ++ ; } } return false ; } function CompareAlphanumericUtil ( str1 , str2 ) { let res ; res = CompareAlphanumeric ( str1 . split ( ' ' ) , str2 . split ( ' ' ) ) ; if ( res == true ) { document . write ( " " + " " ) ; } else { document . write ( " " ) ; } } let str1 , str2 ; str1 = " " ; str2 = " " ; CompareAlphanumericUtil ( str1 , str2 ) ; str1 = " " ; str2 = " " ; CompareAlphanumericUtil ( str1 , str2 ) ;