Find the word from a given sentence having given word as prefix | Function to find the position of the string having word as prefix ; Initialize an ArrayList ; Extract words from the sentence for ( String e : a ) ; Traverse each word ; Traverse the characters of word ; If prefix does not match ; Otherwise ; Return the word ; Return - 1 if not present ; Driver code
function isPrefixOfWord ( sentence , Word ) { var a = sentence . split ( " " ) ; var v = [ ] ; for ( var i = 0 ; i < a . length ; i ++ ) { v . push ( a [ i ] ) ; } for ( var i = 0 ; i < v . length ; i ++ ) { for ( var j = 0 ; j < v [ i ] . length ; j ++ ) { if ( v [ i ] . charAt ( j ) != Word [ j ] ) break ; else if ( j == Word . length - 1 ) return v [ i ] ; } } return " " ; } var s = " " ; var word = " " ; document . write ( isPrefixOfWord ( s , word ) ) ;