+ 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 ?

3rd Mar 2021, 7:04 PM
Rachita Bhasin
36 odpowiedzi
+ 32
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); } }
23rd May 2021, 3:10 PM
sirine elayari
+ 11
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]); } } }
28th May 2021, 4:03 PM
Abhishek Biswas
Abhishek Biswas - avatar
+ 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 String reverse = ""; for(int i = arr.length - 1; i >= 0; i--){ reverse = reverse + arr[i]; } System.out.println(reverse); } }
30th Aug 2021, 3:29 AM
Ankit Tiwari
Ankit Tiwari - avatar
+ 1
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); } }
19th Apr 2022, 4:52 PM
Ajoy Chowdhury
Ajoy Chowdhury - avatar
+ 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){ System.out.print(arr[i]); } } }
6th Jul 2022, 8:18 AM
cyrus mwendwa
cyrus mwendwa - avatar
+ 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(); int len = arr.length; char[] sol = new char[len]; int n = 0; for(int k = arr.length-1; k>=0;k--){ sol[n] = arr[k]; n++; } System.out.println(sol); } }
9th Nov 2022, 7:02 PM
Just Skiper
Just Skiper - avatar
+ 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(); String tmp =""; //your code goes here int t = arr.length -1 ; for (int i = t; i>-1;i--){ tmp+= arr[i] ; } System.out.println(tmp); } }
4th Feb 2023, 1:31 PM
Haitame Laframe
Haitame Laframe - avatar
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); } }
7th May 2022, 11:16 AM
U.L.F. Feroza
0
It can be solved using two pointer approch...loop from start to end and swap the first pointer at the start of string and the last point at the end of string and increment the start pointer and decement the last pointer. Idea of the algorithm Int low = 0; Int high = arr.length -1; for ( int I= 0; I < n; I++){ Swap(arr[low], arr[high]); low++; high--; }
9th Jul 2022, 12:00 PM
Abhishek Biswas
Abhishek Biswas - avatar
0
//your code goes here char[] rvsarr = new char [arr.length]; for(int i =0;i<arr.length;i++){ rvsarr[i] = arr[arr.length-i-1]; } System.out.println(rvsarr);
20th Jul 2022, 4:27 AM
박선경
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 x = arr.length - 1; x >= 0; x-- ) { System.out.print(arr[x]); } } }
15th Aug 2022, 1:21 AM
jomar
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>-1;i--){ System.out.println(arr[i]); } } }
7th Nov 2022, 1:27 PM
Lucian Negură
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--){ System.out.print(arr[i]); } } }
18th Nov 2022, 12:51 PM
karthik v
karthik v - avatar
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 >-1 ; i--) System.out.print(arr[i]); } }
19th Feb 2023, 10:19 AM
Himavarshini Attili
Himavarshini Attili - avatar
0
Input: hello Output: olleh
9th Nov 2023, 8:45 AM
tariq hamza ahmad | AP23111260070
tariq hamza ahmad | AP23111260070 - avatar
- 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[]); } }
3rd Mar 2021, 7:13 PM
Rachita Bhasin
- 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) ;
3rd Mar 2021, 7:18 PM
Bahhaⵣ
Bahhaⵣ - avatar
3rd Mar 2021, 7:30 PM
Jayakrishna 🇮🇳
- 1
Iterate through the char array in reverse order.
3rd Mar 2021, 8:26 PM
Sonic
Sonic - avatar
- 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]);
3rd Mar 2021, 9:15 PM
visph
visph - avatar