- 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
7 odpowiedzi
0
Like this
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));
}
}
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 ^^)
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]);
0
Thanks
0
You're welcome.
- 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