+ 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 ?
35 Answers
+ 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);
}
}
+ 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]);
}
}
}
+ 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);
}
}
+ 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);
}
}
+ 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]);
}
}
}
+ 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);
}
}
+ 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);
}
}
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);
}
}
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--;
}
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);
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]);
}
}
}
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]);
}
}
}
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]);
}
}
}
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]);
}
}
0
Input: hello
Output: olleh
- 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]);