How can I solve this? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

How can I solve this?

Escribe un programa para tomar una cadena como entrada y generar su inverso. El código dado toma una cadena como entrada y la convierte en una matriz de caracteres, que contiene letras de la cadena como sus elementos. Ejemplo de entrada: hello there Ejemplo de salida: ereht olleh

19th Jun 2021, 5:50 PM
laura contreras
laura contreras - avatar
7 Answers
0
Like this
19th Jun 2021, 6:09 PM
laura contreras
laura contreras - avatar
0
import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String text = scanner.nextLine(); char[] arr = text.toCharArray(hello there); //your code goes here char[] result = new char[arr.length]; //store the result in reverse order into the result char[] for(int i=0; i<arr.length; i++) { result[i] = arr[arr.length - i - 1]; } System.out.println(new String(result)); } }
19th Jun 2021, 6:09 PM
laura contreras
laura contreras - avatar
0
almost: you just shouldn't put arguments inside toCharArray() method (and if you have to pass a string as argument, you should enclose it in quotes ^^)
19th Jun 2021, 6:20 PM
visph
visph - avatar
0
char[] arr = text.toCharArray(); //here, dont pass argument. //if you dont need to store and just need to print then do like this, no need of another array... for(int i=arr.length-1;i>=0; i--) System.out.print(arr[i]);
19th Jun 2021, 6:30 PM
Jayakrishna 🇮🇳
0
Thanks
19th Jun 2021, 6:36 PM
laura contreras
laura contreras - avatar
0
You're welcome.
19th Jun 2021, 6:47 PM
Jayakrishna 🇮🇳
- 1
Accept input into a string. Find its length. Print in reverse order like from char at last to first by method charAt(index) Last index is stringlength -1 to first index 0
19th Jun 2021, 5:55 PM
Jayakrishna 🇮🇳