
Asked in Cognizant and 85 others
Write a program to reverse a given string.

String reversal is a common programming task that involves reversing the order of characters in a given string.
Simple Algorithm: Iterate through the string from the end to the beginning, appending eac...read more
import java.util.Scanner;
public class StringReversal {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String inputString = scanner.nextLine();
String reversedString = reverseString(inputString);
System.out.println("Reversed string: " + reversedString);
}
public static String reverseString(String inputString) {
String reversedString = "";
for (int i = inputString.length() - 1; i >= 0; i--) {
reversedString += inputString.charAt(i);
}
return reversedString;
}
}
Interview Questions from Popular Companies










Reviews
Interviews
Salaries
Users

