Reverse a String in Java
Hello and Welcome to The World of Computer Science and Technology
www.TechWithCode.com
Today we will learn about how to write a Java Program to Reverse a String in Java. Before moving forward, we must have knowledge about what is String Java?
What is String in Java?
The char type represents only one character. To represent a string of characters, use the data type called String. For example, the following code declares a message to be a string with the value "Welcome to TechWithCode".
String message = "Welcome to TechWithCode";
The string is a predefined class in the Java library, just like the classes System and Scanner.
The String type is not a primitive type. It is known as a reference type. Any Java class can be used as a reference type for a variable
The reverse of a String:
It refers that shifting the character of a string from left to right or vice-versa
Java Programm of Reverse of String:
There can be many ways to write a program to reverse a string in java but here we will see total four ways to write a java program to reverse a string.
1. Reverse a String in Java using CharAt Method
Problem Statement⤵️
Write a Java program to Reverse a String using CharAt Method.
Source code⤵️
import java.util.*;
public class StringReverse{
public static void main(String args[]) {
String inpt, rev="";
Scanner in=new Scanner(System.in);
System.out.println("Enter the string");
inpt=in.nextLine();
int length=inpt.length();
for(int i=length-1;i>=0;i--)
rev=rev+inpt.charAt(i);
System.out.println("Reversed string: "+rev);
}
}
output⤵️
Enter the string
HELLO TECHWITHCODE
Reversed string: EDOCHTIWHCET OLLEH
2. Reverse a String in Java using String Builder / String Buffer Class
Problem Statement⤵️
Write a Java program to Reverse a String using String Builder / String Buffer Class.
Source code⤵️
import java.util.*;
public class StringRev{
// Function to reverse a string in Java using StringBuilder
public static String rev(String s){
return new StringBuilder(s).reverse().toString();
}
public static void main(String[] args){
String s= "Welcome to TECHWITHCODE";
s= rev(s);
System.out.println("Result after reversing a string is : "+s);
}
}
output⤵️
Result after reversing a string is : ODOCHTIWHCET ot emocleW
3. String Reverse in java using Recursion
Problem Statement⤵️
Write a Java program to String Reverse using Recursion
Source code⤵️
import java.util.*;
public class StringRecursion{
String rev(String str) {
if(str.length() == 0)
return " ";
return str.charAt(str.length()-1) + rev(str.substring(0,str.length()-1)); }
public static void main(String[ ] args) {
StringRecursion r=new StringRecursion();
Scanner sc=new Scanner(System.in);
System.out.print("Enter the string : ");
String s=sc.nextLine();
System.out.println("Reversed String: "+r.rev(s)); }
}
output⤵️
Enter the string : TechWithCode is best CSE site
Reversed String: etis ESC si edoChtiWhceT
4. Reversing a String in java using Reverse Iteration
Problem Statement⤵️
Write a Java program to Reverse a String using Reverse Iteration.
Source code⤵️
import java.util.*;
public class StringRev{
// Function to reverse a string in Java
public static String reverseString(String s){
//Converting the string into a character array
char c[]=s.toCharArray();
String reverse="";
//For loop to reverse a string
for(int i=c.length-1;i>=0;i--){
reverse+=c[i];
}
return reverse;
}
public static void main(String[] args) {
System.out.println(reverseString("Hi All"));
System.out.println(reverseString("Welcome to TechWithcode"));
}
}
output⤵️
llA iH
edoChtiWhceT ot emocleW
Your blog contains lots of valuable data about website design seattle. It is a factual and beneficial article for us. Thankful to you for sharing an article like this.
ReplyDelete