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

0

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

26 Answers

New Answer

+20

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); } }

+6

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]); } } }

+4

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

0

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

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(); //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); } }

-1

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[]); } }

-1

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) ;

-1

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

-1

Iterate through the char array in reverse order.

-1

// 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]);

-1

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 } }

-1

Thank you all for help guys

-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); } }

-1

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]); } } }

-1

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]); } } }

-1

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]); }

-1

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); } } }