Reverse a String Without Built-In Functions

E · easyP · Verified PYQstrings

Reverse a given string without using any built-in reverse functions or slicing.

Input
Hello World
Output
dlroW olleH
Use a loop from len(s)-1 down to 0, build result string. Or swap in-place with two pointers.
javamay contain transcription errors
import java.util.*;
class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String rev = "";
for(int i = str.length() - 1; i >= 0; i--)
rev += str.charAt(i);
System.out.println(rev);
}
}
← Reverse a NumberReverse Words in a Sentence →
Report an issue with this question