-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathReverseString.java
37 lines (30 loc) · 1012 Bytes
/
ReverseString.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package interviewprograms;
import java.util.Scanner;
/**
*
* @author HASAN
*/
public class ReverseString {
public static void main(String[] args) {
System.out.println("Enter String to be reversed: ");
Scanner sc= new Scanner(System.in);
String string= sc.nextLine();
String reverse="";
for(int i=string.length()-1; i>=0; i--){
reverse= reverse+string.charAt(i); //The method charAt(int index) returns the character at the specified index
}
System.out.println("After reversing: "+ reverse);
//Another way
System.out.println("\nAnother way-->");
StringBuffer a = new StringBuffer("Java programming is fun");
System.out.println(a.reverse());
}
}
/*
run:
Enter String to be reversed:
ajmal hasan
After reversing: nasah lamja
Another way-->
nuf si gnimmargorp avaJ
*/