0

Reverse of a string can any body tell me how can I do this ?

.

3rd Mar 2021, 7:02 PM
Rachita Bhasin
8 Respuestas
0
//corrected code , read comments for corrections explanation. Hope it helps.. 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 ; you must initialize array size in java. char[] c = new char[arr.length]; //size of arr.length is enough //your code goes here for(int i=arr.length-1;i>=0;i--) // max index value is arr.length-1 since index starts from 0 (not from 1). c[i]=arr[i]; // you must give index value or subcript in c[] for(int i=0;i<arr.length;i++) System.out.print(c[i]);// same here must give index value } }
3rd Mar 2021, 7:27 PM
Jayakrishna 🇮🇳
+ 4
Mention the relevant language name in tags please. if the string is "hello", then take the length of it , i.e.5 and run a for loop with 5-1 as initial value all the way down to 0 and at the print the character at that index
3rd Mar 2021, 7:06 PM
Abhay
Abhay - avatar
+ 2
import java.util.Scanner; class reverseofaString { public static void main(String[ ] arg) { String str; Scanner scan=new Scanner(System.in); System.out.print("Enter a string : "); str=scan.nextLine(); char[] ch=str.toCharArray(); System.out.println("Reverse of a String is :"); int j=ch.length; for(int i=j;i>0;i--) { System.out.print(ch[i-1]); } } }
4th Mar 2021, 8:39 PM
Shakila
Shakila - 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
Iterate through the char array in reverse order.
3rd Mar 2021, 8:27 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
0
In java how can I convert string into character
3rd Mar 2021, 7:07 PM
Rachita Bhasin