0

This program should return " EIOUA". Note: Use replace() function.

https://code.sololearn.com/caPB78HAhDDC/?ref=app

28th Jun 2021, 10:46 AM
ITZ THE GAME CHANGER
3 odpowiedzi
+ 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 ;)
28th Jun 2021, 10:56 AM
visph
visph - avatar
+ 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....
28th Jun 2021, 11:53 AM
Jayakrishna 🇮🇳
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.
28th Jun 2021, 12:10 PM
Oma Falk
Oma Falk - avatar