0% found this document useful (0 votes)
74 views

Reverse A String

The document contains code examples for reversing a string in Java using different approaches: 1. Iterating through the string from the end and adding characters to a new string 2. Converting the string to a character array and iterating in reverse order 3. Swapping characters at the start and end indexes of a character array 4. Converting the string to a byte array and swapping bytes 5. Using the reverse() method on a StringBuffer 6. Implementing string reversal recursively

Uploaded by

sharif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views

Reverse A String

The document contains code examples for reversing a string in Java using different approaches: 1. Iterating through the string from the end and adding characters to a new string 2. Converting the string to a character array and iterating in reverse order 3. Swapping characters at the start and end indexes of a character array 4. Converting the string to a byte array and swapping bytes 5. Using the reverse() method on a StringBuffer 6. Implementing string reversal recursively

Uploaded by

sharif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

package com.kkjavatutorials.

client;
import java.util.Scanner;
/**
* @author KK JavaTutorials
*Java program to Reverse a String
*/
public class ReverseStringTest1 {

public static void main(String[] args) {

try(Scanner scanner = new Scanner(System.in)) {


//Taking input from keyboard using Scanner
System.out.println("Enter string which you want to reverse::");
String inputText = scanner.next();
String reversedString =
reverseStringUsingForwardLogic(inputText);
System.out.println("Reversed String:" + reversedString);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}

private static String reverseStringUsingForwardLogic(String inputText) {


// base case if input is null or empty
if (inputText == null || inputText.trim().isEmpty())
return inputText;

String reverse = "";


for (int i = inputText.length()-1; i>=0 ; i--) {
char c = inputText.charAt(i);
reverse = reverse+c;
}
return reverse;
}
}
--------------------------------------------------
package com.kkjavatutorials.client;
import java.util.Scanner;
/**
* @author KK JavaTutorials
*Java program to Reverse a String
*/
public class ReverseStringTest2 {

public static void main(String[] args) {

try(Scanner scanner = new Scanner(System.in)) {


//Taking input from keyboard using Scanner
System.out.println("Enter string which you want to reverse::");
String inputText = scanner.next();
String reversedString =
reverseStringUsingForwardLogic(inputText);
System.out.println("Reversed String:" + reversedString);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}

private static String reverseStringUsingForwardLogic(String inputText) {


// base case if input is null or empty
if (inputText == null || inputText.trim().isEmpty())
return inputText;

String reverse = "";


//Convert string into character array
char[] charArray = inputText.toCharArray();
for (int i = inputText.length()-1; i>=0 ; i--) {
reverse +=charArray[i];
}
return reverse;
}
}
------------------------------------

package com.kkjavatutorials.client;
import java.util.Scanner;
/**
* @author KK JavaTutorials
*Java program to Reverse a String
*/
public class ReverseStringTest4 {

public static void main(String[] args) {

try (Scanner scanner = new Scanner(System.in)) {


//Taking input from keyboard using Scanner
System.out.println("Enter string which you want to reverse::");
String inputText = scanner.next();
String reversedString =
reverseStringUsingForwardLogic(inputText);
System.out.println("Reversed String:" + reversedString);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}

private static String reverseStringUsingForwardLogic(String inputText) {


// base case if input is null or empty
if (inputText == null || inputText.trim().isEmpty())
return inputText;

//Convert string into character array


char[] inputByteArray = inputText.toCharArray();
int left, right = inputByteArray.length - 1;
for (left = 0; left < right; left++, right--) {
// Swap values of left and right index
char temp = inputByteArray[left];
inputByteArray[left] = inputByteArray[right];
inputByteArray[right] = temp;
}
String reverseString = new String(inputByteArray);
return reverseString.toString();
}
}
------------------------------------------

package com.kkjavatutorials.client;
import java.util.Scanner;
/**
* @author KK JavaTutorials
*Java program to Reverse a String using recursion
*/
public class ReverseStringTest6 {

public static void main(String[] args) {

try(Scanner scanner = new Scanner(System.in)) {


//Taking input from keyboard using Scanner
System.out.println("Enter string which you want to reverse::");
String inputText = scanner.next();
String reversedString = reverseString(inputText);
System.out.println("Reversed String:" + reversedString);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}

private static String reverseString(String inputText) {


// base case if input is null or empty
if (inputText == null || inputText.trim().isEmpty())
return inputText;
//Calling Function Recursively
return reverseString(inputText.substring(1)) + inputText.charAt(0);
}
}
-------------------------------------------
import java.util.Scanner;
public class ReverseStringTest3 {

public static void main(String[] args) {

try(Scanner scanner = new Scanner(System.in)) {


//Taking input from keyboard using Scanner
System.out.println("Enter string which you want to reverse::");
String inputText = scanner.next();
String reversedString =
reverseStringUsingForwardLogic(inputText);
System.out.println("Reversed String:" + reversedString);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}

private static String reverseStringUsingForwardLogic(String inputText) {


// base case if input is null or empty
if (inputText == null || inputText.trim().isEmpty())
return inputText;

//Convert string into byte array


byte[] bytes = inputText.getBytes();
byte []revByteArr = new byte[bytes.length];
for (int i = 0; i < bytes.length; i++) {
revByteArr[i] = bytes[bytes.length-i-1];
}
String reverseString = new String(revByteArr);
return reverseString;
}
}

-------------------------
package com.kkjavatutorials.client;
import java.util.Scanner;
/**
* @author KK JavaTutorials
*Java program to Reverse a String
*/
public class ReverseStringTest5 {

public static void main(String[] args) {

try(Scanner scanner = new Scanner(System.in)) {


//Taking input from keyboard using Scanner
System.out.println("Enter string which you want to reverse::");
String inputText = scanner.next();
String reversedString = reverseStringUsingBuffer(inputText);
System.out.println("Reversed String:" + reversedString);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}

private static String reverseStringUsingBuffer(String inputText) {


// base case if input is null or empty
if (inputText == null || inputText.trim().isEmpty())
return inputText;
//Making use of StringBuffer reverse() method
StringBuffer sb = new StringBuffer(inputText);
sb.reverse();
return sb.toString();
}
}

You might also like