Hi guys I'm new in java can anyone help me to print out a reversed string this my code: | Sololearn: Learn to code for FREE!

+1

Hi guys I'm new in java can anyone help me to print out a reversed string this my code:

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(); int i,j; j=text.length; for(i=0;i<arr.length;i++){ arr[i]=text[j]; --j; } for(i=0;i<arr.length;i++){ System.out.print(arr[i]); } } }

9/16/2021 10:25:27 AM

Coderalgeria

14 Answers

New Answer

+7

This is a question from the Java course and it is a terrible example. It displays a total lack of understanding of Java. Though the purpose of the exercise is to demonstrate loops and arrays, not to reverse a string. To print the array in reverse you iterate over the array as suggested by Rik Wittkopp. Simply use the char array length less 1 to initialise the loop. The exercise even states... "You can loop through the char array, starting from the end, using arr.length to get the size of the array." Since arrays use a 0 based index the last character will be found at arr.length() - 1. Thus only a single loop is required after the creation of the char array... char[] arr = text.toCharArray(); for(int i = arr.length()-1; i>=0; i--) { System.out.print(arr[i]); } There is absolutely no need for the char array in this exercise, which is why I think this a terrible example. The code could have been written without even creating the char array as follows.. String text = scanner.nextLine(); for(int i = text.length()-1; i>=0; i--) { System.out.print(text.charAt[i]); } Note that neither of these techniques actually reverses the string, they simply print it in reverse. It must also be remembered that the exercise is in the Arrays section of the course; classes and objects have yet to be covered. To reverse a string there is the reverse method in the StringBuffer class, which is the sane way to reverse a string. Rather than creating a char array from the entered string a StringBuffer object could have been created... StringBuffer sb = new StringBuffer(text); System.out.println(sb.reverse()); I think SoloLearn could have used a clearer example in this case.

+4

just replace String sample = "Sample string"; with String sample = new Scanner(System.in).nextLine(); for getting input

+3

Try iterating through your input in reverse with your for loop. Example: public class Program { public static void main(String[] args) { for(int i=9; i>=0; i--){ System.out.println(i); } } }

+3

String text = sc.nextLine(); StringBuilder sb = new StringBuilder(text); System.out.println(sb.reverse().toString());

+3

Martin Taylor Niththish I see thank u all ,I really apericiate it

+2

GHOST mHBr thank u

+2

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(); int i; for(i=arr.length-1;i>=0;i--){ System.out.print(arr[i]); } } }

+2

import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner in = new Scanner(System.in); String text = in.nextLine(); for (int i=text.length()-1;i>=0;i--) System.out.print(text.charAt(i)); } }

+1

reverse() is the key! But you can do this by yourself: something like this: String sample = "Sample string"; String rev = ""; int r=0; for (int i=0 ; i < sample.length() ; i++){ r = sample.length()-i; rev.charAt(i) = sample.charAt(r); { System.out.println(rev);

+1

This will work: public class Program { public static void main(String[] args){ String sample = "Sample string"; char rev[] = sample.toCharArray(); int r=0; for (int i=0 ; i < sample.length() ; i++){ r = sample.length()-i-1; rev[i] = sample.charAt(r); } System.out.println(rev); } }

+1

GHOST mHBr I know but I want the user to input the string!

+1

Something like this: https://code.sololearn.com/c4y31h1S2K16/?ref=app

+1

https://code.sololearn.com/cAs5zSe2QVoW/?ref=app

0

Rik Wittkopp when i compile it , an error message appears indicating there is a problem in line 12