Program to replace every space in a string with hyphen | Get the String ; Traverse the string character by character . ; Changing the ith character to ' - ' if it 's a space. ; Print the modified string .
< ? php $ str = " A ▁ computer ▁ science ▁ portal ▁ for ▁ geeks " ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; ++ $ i ) { if ( $ str [ $ i ] == ' ▁ ' ) { $ str [ $ i ] = ' - ' ; } } echo $ str . " STRNEWLINE " ;