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

This program reverses a given string using various methods in programming languages.
Use a loop to iterate through the string backwards. Example: 'hello' becomes 'olleh'.
Utilize built-in functions like...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

