Created
December 12, 2021 07:06
-
-
Save Neptune998/eaa7e58d12de954810af59f1b8fdc7ce to your computer and use it in GitHub Desktop.
String Reversal Using CharAt Method in Java.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.util.Scanner; | |
//java program to reverse a word | |
class ReverseString { | |
public static void main(String[] args) { | |
String oriString = "NeptuneWorld"; | |
String revString = ""; | |
char ch; | |
System.out.println("String: " + oriString); | |
for (int i = 0; i < oriString.length(); i++) { | |
ch = oriString.charAt(i); // extracts character at ith location | |
revString = ch + revString; // add extract character at the given location | |
} | |
System.out.println("Reversed String : " + revString); | |
} | |
} | |
//Output: | |
//String: NeptuneWorld | |
//Reversed String : dlroWenutpeN |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment