Sunday, March 15, 2020

How to reverse a String in java

Write a java program to reverse the String. There are to reverse a String in java. You should know at least how to reverse a String without using reverse() method in java.
How to reverse a String in java

Logic Reverse a string in java:

1. The user will input the string to be reversed.
2. First we will convert String to character array by using the built in java String class method toCharArray().
3. Then , we will scan the string from end  to start,  and print the character one by one.

Code Reverse string in java

import java.io.*;
import java.util.*;
public class reverseString {
 public static void main(String[] args) {
  String input="Codejavaexample";
  StringBuilder input1 = new StringBuilder();
  input1.append(input);
  input1=input1.reverse();
  for (int i=0;i<input1.length();i++)
  System.out.print(input1.charAt(i)); 
 }}
Ouput:
elpmaxeavajedoc

No comments:

Post a Comment