0
This program should return " EIOUA". Note: Use replace() function.
3 Respostas
+ 3
don't use replace, rather create a new (empty) string before the loop, and append char to it according to the char encountered in each iteration ;)
+ 1
There replace() method replaces all occurrences of first charecter with replace charecter. So after 1st replace of 'E' of 'A', you have string z="EEIOU" but next replace of 'I' of 'E' make it as IIIOU..
(edit : oh here you using original string so last replacement only will be saved in variable z ie z=s.replace('U','A'); so z = "AEIOA". so all previous modifications are lost with this replacement, in each iteration.)
So you are having multiple replacement, expecting in each iteration, at a time , .. not as you expecting..
Instead you need to rotate string once. Or append first charecter to end, and replace first charecter by empty string "". No need of loop.
hope it helps....
0
Try this pattern
if ( ch== 'A'){
s=s.substring(0,a)+s.substring(a).replace('A','E');
continue;}
After print s and not z
it only works because each char occurs once.
otherwise it would fail if u dont use replacfirst.