Java program to reverse a string

Java program to reverse a string:
In this page, you are going to learn how to reverse the string using various methods. The methods are char charAt(),int length(), char[] toCharArray(), byte[] getBytes(), StringBuffer append(). To reverse the words in a sentence String[] split() method is used.

java,reverse a number in java,java program,reverse a string in java,java program to reverse a string,reverse,reverse string,reverse a string,java program to reverse a string without using api,program to reverse a string,reverse string in java,string,how to reverse a string in java,string reverse in java,how to reverese a string in java,how to reverse a string,java (programming language)



Example 1:
// using the int length()  and charAt() method

import java.util.Scanner;
public class Example1
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the string: ");
String s = sc.nextLine();
System.out.println("Before reversing the string: " + s);
String reverse = "";
int n = s.length();
for(int i =n-1; i>=0; i--)
{
reverse = reverse + s.charAt(i);
}
System.out.println("After reversing a string: " + reverse);
}
}

Output:
Enter the string: Java Program
Before reversing the string: Java Program
After reversing a string: margorP avaJ

Example 2:
// using char[] toCharArray() method

import java.util.Scanner;
public class Example2
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the string: ");
String s = sc.nextLine();
System.out.println("Before reversing the string using toCharArray method: " + s);
String reverse = "";
char[] ch = s.toCharArray();
int n = ch.length;
for(int i=(n-1); i>=0;i--)
{
reverse = reverse + ch[i];
}
System.out.println("After reversing the string: " + reverse);
}
}

Output:
Enter the string: Java Program
Before reversing the string using toCharArray method: Java Program
After reversing the string: margorP avaJ

Example 3:
//using byte[] getBytes() method

import java.util.Scanner;
public class Example3
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the string: ");
String s = sc.nextLine();
System.out.println("Before reversing the string using getBytes() method: " + s);
byte[] bt = s.getBytes();
char[] c = new char[bt.length];
for(int i=0;i<(bt.length);i++)
{
c[i] = (char)bt[(bt.length)-i-1];
}
System.out.println("After reversing the String: " + new String(c));
}
}

Output:
Enter the string: Java Program
Before reversing the string using getBytes() method: Java Program
After reversing the String: margorP avaJ

Example 4:
// using StringBuffer append() method

import java.util.Scanner;
public class Example4
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the string: ");
String s = sc.nextLine();
System.out.println("Before reversing the string using StringBuffer append() method: " + s);
// you can either use StringBuilder append() method
StringBuffer sb = new StringBuffer();
for(int i=(s.length()-1);i>=0;i--)
{
sb.append(s.charAt(i));
}
System.out.println("After reversing the String: " + sb.toString());
}
}

Output:
Enter the string: Java Program
Before reversing the string using StringBuffer append() method: Java Program
After reversing the String: margorP avaJ

Example 5:
// Using String[] split() method - to reverse the words in a sentence.

import java.util.Scanner;
public class Example5
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the sentence: ");
String s = sc.nextLine();
System.out.println("Before reversing using split() method: " + s);
String[] sp = s.split(" ");
StringBuffer sb = new StringBuffer();
for(int i = (sp.length - 1); i>=0;i--)
{
sb.append(sp[i]+" ");
}
System.out.println("After reversing: " + sb.toString());

}
}

Output:
Enter the sentence: This is a java program
Before reversing using split() method: This is a java program
After reversing: program java a is This


Comments

Popular posts from this blog

Multiple image upload to database and display from it

PHP Login and Registration script

String methods in java