-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseString.java
More file actions
30 lines (23 loc) · 918 Bytes
/
ReverseString.java
File metadata and controls
30 lines (23 loc) · 918 Bytes
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
import java.util.Scanner;
public class ReverseString {
/*Program to reverse the order of characters in any given string.*/
public static void main(String[] args) {
System.out.print("Enter string:");
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
sc.close();
System.out.println("Reversed string is: "+Reverse(str)); // By custom method
StringBuilder builder = new StringBuilder(str);
builder.reverse();
System.out.println("By StringBuilder: "+builder.toString()); // By StringBuilder
}
static String Reverse(String str){
String newStr = "";
int temp = 1;
for(int i=0; i<str.length(); i++){
newStr += str.charAt(str.length()-temp);
temp ++;
}
return newStr;
}
}