Replace all occurrences of pi with 3.14 in a given string | Function to replace all occurrences of pi in a given with 3.14 ; Iterate through second last element of the string ; If current and current + 1 alphabets form the word ' pi ' append 3.14 to output ; Append the current letter ; Return the output string ; Driver Code
function replacePi ( input ) { var output = " " ; var size = input . length ; for ( var i = 0 ; i < size ; ++ i ) { if ( i + 1 < size && input [ i ] === " " && input [ i + 1 ] === " " ) { output += " " ; i ++ ; } else { output += input [ i ] ; } } return output ; } var input = " " ; document . write ( replacePi ( input ) ) ;