Reverse of a string | Sololearn: Learn to code for FREE!

+1

Reverse of a string

Write a program to take a string as input and output its reverse. The given code takes a string as input and converts it into a char array, which contains letters of the string as its elements. Sample Input: hello there Sample Output: ereht olleh can any body tell how i can do this ?

.

3/3/2021 7:04:56 PM

Rachita Bhasin

19 Answers

New Answer

+9

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(); //your code goes here int n=arr.length; String str=""; for(int i=n-1 ; i>-1 ; i--){ str=str+arr[i]; } System.out.println(str); } }

+4

try this code it will works 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(); //your code goes here for(int i = 0; i < arr.length; i++){ System.out.print(arr[arr.length - i - 1]); } } }

+3

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(); char[] c; //your code goes here for(int i=arr.length;i>-1;i--) System.out.print(c[i]) }

+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(); //your code goes here String reverse = ""; for(int i = arr.length - 1; i >= 0; i--){ reverse = reverse + arr[i]; } System.out.println(reverse); } }

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(); char[] c; //your code goes here for(int i=arr.length;i>0;i--) c[]=arr[i]; for(int i=0;i<arr.length;i++) System.out.print(c[]); } }

0

change char[] c to a String and construct it in the loop. String str = ""; for(int i = arr.length-1; i >= 0; i--){ str = str + arr[i]; } System.out.println(str) ;

0

Pls don't create duplicates.. https://www.sololearn.com/Discuss/2713428/?ref=app

0

Iterate through the char array in reverse order.

0

// first, you should initialize your 'c' char array: char[] c = new char[arr.length]; // then you should acess array items by giving index in square brackets (so, you doesn't need to iterate backwards:'): for (int i=0; i<arr.length; ++i) c[i] = arr[arr.length-i-1]; // also in second loop: System.out.print(c[i]);

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(); for(int i=arr.length-1;i>=0;i--) { System.out.println(arr[i]); }// Just we start printing from back side } }

0

Thank you all for help guys

0

import java.util.Scanner; public class Program { public static void main(String[] args) { //code by jeho Scanner scanner = new Scanner(System.in); String text = scanner.nextLine(); char[] arr = text.toCharArray(); for(int i=arr.length-1;i>=0;i--) { System.out.println(arr[i]); } } }

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(); //Please Subscribe to My Youtube Channel //Channel Name: Fazal Tuts4U for(int i=arr.length-1; i>-1; i--){ System.out.print(arr[i]); } } }

0

Try this on Inteliji public static void main(String[] args) { Scanner text = new Scanner(System.in); String input; System.out.println("Enter the your value in letter"); input =text.nextLine(); char[] output = input.toCharArray(); for (int i = output.length -1; i>=0; i--) { System.out.println(output[i]); }

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(); //your code goes here for(int i=arr.length-1; i>=0; i--){ char c = arr[i]; System.out.print(c); } } }

-1

This works! 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(); //your code goes here String rev=" "; for(int i=arr.length-1;i>=0;i--){ rev=rev+arr[i]; } System.out.println(rev); } }

-2

Thanks to all for solving my query

-3

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

-4

post your attempt and/or specify the programming language in tags.