Count the Number of matching characters in a pair of strings | Function to count the matching characters ; Traverse the string 1 char by char ; This will check if str1 [ i ] is present in str2 or not str2 . find ( str1 [ i ] ) returns - 1 if not found otherwise it returns the starting occurrence index of that character in str2 ; Driver code
function count ( str1 , str2 ) { var c = 0 ; for ( var i = 0 ; i < str1 . length ; i ++ ) { if ( str2 . includes ( str1 [ i ] ) ) c += 1 ; } document . write ( " " + + parseInt ( c ) ) ; } var str1 = " " ; var str2 = " " ; count ( str1 , str2 ) ;