Java Online Compiler
Example: Reverse String using charAt and Concatenation in Java
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// Reverse String using charAt and Concatenation public class Main { public static void main(String[] args) { String originalString = "hello"; String reversedString = ""; // Step 1: Iterate through the original string from the beginning to the end for (int i = 0; i < originalString.length(); i++) { // Step 2: Get the character at the current index char character = originalString.charAt(i); // Step 3: Prepend the character to the reversed string reversedString = character + reversedString; } System.out.println("Original String: " + originalString); System.out.println("Reversed String: " + reversedString); } }
Output
Clear
ADVERTISEMENTS