Java Online Compiler
Example: String Reversal using charAt() in Java
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// String Reversal using charAt() public class Main { public static void main(String[] args) { String originalString = "programming"; String reversedString = ""; // Step 1: Iterate from the last character to the first for (int i = originalString.length() - 1; i >= 0; i--) { // Step 2: Get the character at the current index char c = originalString.charAt(i); // Step 3: Append the character to the reversed string reversedString += c; } System.out.println("Original String: " + originalString); System.out.println("Reversed String: " + reversedString); } }
Output
Clear
ADVERTISEMENTS