Skip to main content

String Questions

1. Different ways to reverse a string

Description: Reverse the given string and print the characters in reverse order.
Input: Code Java Mastery
Output: yretsaM avaJ eroC

Reverse a String with For Loop

public class ReverseString1 {
public static void main(String arg[]){
String str="Core Java Mastery";
String rev="";
for(int i=str.length()-1; i>=0; i--){
rev = rev + str.charAt(i);
}
System.out.println("Reversed string is: "+ rev);
}
}

Reverse a String with While Loop

public class ReverseString2 {
public static void main(String[] args) {
String str="Core Java Mastery";
String rev="";
int i = str.length()-1;
while(i>=0){
rev = rev+str.charAt(i);
i--;
}
System.out.println(rev);
}
}

Reverse a String with do while Loop

public class ReverseString12 {
public static void main(String[] args) {
String str = "Core Java Mastery";
String rev = "";

int i = str.length() - 1;

do {
rev = rev + str.charAt(i);
i--;
} while (i >= 0);

System.out.println("Reversed string is: " + rev);
}
}

Reverse a String with StringBuilder reverse method

public class ReverseString3 {
public static void main(String[] args) {
String str = "Core Java Mastery";
String rev = new StringBuilder(str).reverse().toString();
System.out.println("Reversed string is: " + rev);
}
}

Reverse a String with char array

public class ReverseString4 {
public static void main(String[] args) {
String str = "Core Java Mastery";

char[] arr = str.toCharArray();

for(int i = arr.length - 1; i >= 0; i--) {
System.out.print(arr[i]);
}
}
}

Reverse a String with recursion

public class ReverseString5 {

public static String reverse(String str) {
if(str.isEmpty())
return str;

return reverse(str.substring(1)) + str.charAt(0);
}

public static void main(String[] args) {
String str = "Core Java Mastery";

System.out.println("Reversed string is: " + reverse(str));
}
}

Reverse a String with stream

public class ReverseString6 {
public static void main(String[] args) {
String str = "Core Java Mastery";

String reversed = IntStream.range(0, str.length())
.mapToObj(i -> str.charAt(str.length() - 1 - i))
.collect(StringBuilder::new,
StringBuilder::append,
StringBuilder::append)
.toString();

System.out.println("Reversed string is: " + reversed);
}
}

Reverse a String with collection reverse

public class ReverseString7 {
public static void main(String[] args) {
String str = "Core Java Mastery";

List<Character> list = new ArrayList<>();

for(char c : str.toCharArray()) {
list.add(c);
}

Collections.reverse(list);

StringBuilder rev = new StringBuilder();
for(char c : list) {
rev.append(c);
}

System.out.println("Reversed string is: " + rev);
}
}

Reverse a String with StringBuffer

public class ReverseString8 {
public static void main(String[] args) {
String str = "Core Java Mastery";

String reversed = new StringBuffer(str).reverse().toString();

System.out.println(reversed);
}
}

Reverse a String Using Two Pointer Technique (Swap)

public class ReverseString9 {
public static void main(String[] args) {
String str = "Core Java Mastery";

char[] arr = str.toCharArray();

int start = 0;
int end = arr.length - 1;

while(start < end) {
char temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;

start++;
end--;
}

System.out.println(new String(arr));
}
}

Reverse a String Using Stack

public class ReverseString10 {
public static void main(String[] args) {
String str = "Core Java Mastery";

Stack<Character> stack = new Stack<>();

for(char c : str.toCharArray()) {
stack.push(c);
}

while(!stack.isEmpty()) {
System.out.print(stack.pop());
}
}
}

Reverse a String Using reduce() in Stream

public class ReverseString11 {
public static void main(String[] args) {
String str = "Core Java Mastery";

String reversed = Stream.of(str.split(""))
.reduce("", (a, b) -> b + a);

System.out.println(reversed);
}
}