0

To reverse the String by implementing the replace() method in java

I'm try to do it but yet not be able to figure it out any solution for it. Here is my attempt !!! https://code.sololearn.com/cJv45cV3gsPj/?ref=app

16th Oct 2019, 5:25 AM
Agron
Agron - avatar
13 Antworten
+ 1
public class Program { public static void main(String[] args) { String x = "Trial Letter"; char[] y = x.toCharArray(); int size = y.length; char[] a = new char[size]; int i = 0; while(i != size){ a[i] = y[size - 1 - i]; ++i; } String reverse = x.replace(x,String.valueOf(a)); System.out.println(x); System.out.println(a); System.out.println(reverse); } }
23rd Oct 2019, 1:53 PM
Grid Osei
Grid Osei - avatar
+ 4
Check this. Done by Me. We can do in two way. https://code.sololearn.com/cuwF40LI0114/?ref=app
16th Oct 2019, 7:27 AM
A͢J
A͢J - avatar
+ 2
Agron Check your while loop. It will be like this while(i != size) { a[i] = y[size - 1 - i]; i++; }
16th Oct 2019, 8:47 AM
A͢J
A͢J - avatar
+ 1
you're using different kind of space. i clean up the code a bit, but its same as yours https://code.sololearn.com/cOG64U9R9Os7/?ref=app
16th Oct 2019, 6:55 AM
Taste
Taste - avatar
+ 1
you're on the track with your code with those commented lines.
16th Oct 2019, 8:42 AM
Taste
Taste - avatar
+ 1
if each character are unique, you can use my first answer. but it isnt, the only way i can think of by using your method which you almost got it. copy the string, reverse it. use replace with it.
16th Oct 2019, 8:51 AM
Taste
Taste - avatar
0
replace char at 0 with chat at n then char at 1 with char at n-1 etc. depend on the string this may not work
16th Oct 2019, 5:42 AM
Taste
Taste - avatar
0
i check your code. you're using nbsp instead of normal space. its funny that non breaking space is actually breaking the program
16th Oct 2019, 6:04 AM
Taste
Taste - avatar
0
So far i have tried to, make changes with different solutions. But still not be able to got the expected output. It's weird that on the mobile version, it gives me compilation error. But on web version its runs nicely.
16th Oct 2019, 6:34 AM
Agron
Agron - avatar
0
Thank you guys !!! Those approaches are nice & clean. just to shair that, For there my main purpose is to incorporate replace() method in it, so to perform the string reverse in it
16th Oct 2019, 8:38 AM
Agron
Agron - avatar
0
to be honest, i would like to know how we can achieve it by replacement method on it. Sorry for being for nastier here guys 😓
16th Oct 2019, 8:47 AM
Agron
Agron - avatar
0
It's true but I'm not be able to got the way, to make it. Still trying to make some changes, if it's not gone work. Then i will try to do it later. But i will make it. Because i wanted to know how to make it possible by this.
16th Oct 2019, 9:12 AM
Agron
Agron - avatar
0
I have obtain the correct solution for it. Thank you so much guys for all of your help here.
22nd Oct 2019, 2:29 AM
Agron
Agron - avatar