How can i solve the third proyect of Java? (Reverse String) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How can i solve the third proyect of Java? (Reverse String)

This is my actual code: It's modified to work with a specific string, "How are you", the program should reverse the string, but doesn't work. If i erase the caracter "?" the program works correctly, what's the problem? :( //import java.util.Scanner; public class program{ public static void main(String[] args){ //Scanner scanner = new Scanner(System.in); System.out.println("Inserta un texto a invertir"); String text = "How are you?";//scanner.nextLine(); char[] arr = text.toCharArray(); for(int i= 0; i< arr.length -1; i++){ if(i == arr.length-(i+1)){ break; } char cambio1 = arr[arr.length-(i+1)]; char cambio2 = arr[i]; arr[arr.length-(i+1)] = cambio2; arr[i] = cambio1; } for(int i= 0; i<= arr.length -1; i++){ System.out.println(arr[i]); } } }

31st Aug 2021, 6:19 PM
Ian López
2 Answers
+ 2
Ian López 1 - Don't print anything except result 2 - just print char array in reverse order which will be reverse string
31st Aug 2021, 6:41 PM
A͢J
A͢J - avatar
0
java is a terrible language so the solution would be using c++, c or python
1st Sep 2021, 10:23 PM
Spritzenkopf
Spritzenkopf - avatar