Replace a character c1 with c2 and c2 with c1 in a string S | PHP program to replace c1 with c2 and c2 with c1 ; loop to traverse in the string ; check for c1 and replace ; check for c2 and replace ; Driver Code
< ? php function replace ( $ s , $ c1 , $ c2 ) { $ l = strlen ( $ s ) ; for ( $ i = 0 ; $ i < $ l ; $ i ++ ) { if ( $ s [ $ i ] == $ c1 ) $ s [ $ i ] = $ c2 ; else if ( $ s [ $ i ] == $ c2 ) $ s [ $ i ] = $ c1 ; } return $ s ; } $ s = " grrksfoegrrks " ; $ c1 = ' e ' ; $ c2 = ' r ' ; echo replace ( $ s , $ c1 , $ c2 ) ; ? >