0

Can you help me this exercise

Write a program that uses a string as input and outputs it backwards in the result. This code takes a string and translates it into a char array. Example of input data: hello there Example result: ereht olleh

26th Jan 2022, 12:51 PM
df dfhg
5 Antworten
+ 3
Please link your code attempt. Hint: use a loop
26th Jan 2022, 12:57 PM
Lisa
Lisa - avatar
+ 1
Ralf Schoenian why 2 loops? Anyways, I think it would be more helpful if you explained how you approached the problem instead of just giving ready-made code
26th Jan 2022, 2:50 PM
Lisa
Lisa - avatar
0
Just print in reverse order
26th Jan 2022, 1:33 PM
A͢J
A͢J - avatar
0
Try this: package playground; public class ReverseString { public static void main(String[] args) { String myString = "Hello there"; char[] result = new char[myString.length()]; int counter = 0; for (int i=myString.length()-1; i>=0; i--) { result[counter] = myString.charAt(i); counter++; } for (int i=0; i < result.length;i++) { System.out.print(result[i]); } } }
26th Jan 2022, 2:46 PM
Ralf Schoenian
Ralf Schoenian - avatar
0
@Lisa, of course, you're right. One loop would be enough, but on the other hand "df dfhg" was asked to use an array.
26th Jan 2022, 4:19 PM
Ralf Schoenian
Ralf Schoenian - avatar