-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrevstring.java
More file actions
25 lines (23 loc) · 759 Bytes
/
revstring.java
File metadata and controls
25 lines (23 loc) · 759 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
// Write a code to reverse a C-style String
// (i.e C-style:abcd is represented as 5 characters including the null character)
//NOTE: .charAt(index),setCharAt(index,charactertobeset)
public class revstring{
public static void main(String[] args){
String ques="wellhellothere";
String ans=reverse(ques);
System.out.println(ans);
}
//you can simply call the reverse method on a string buffer
//basically swapping characters from either ends
static String reverse(String a){
StringBuilder s=new StringBuilder(a);
int length=s.length();
for(int i=0;i<length/2;i++){
char current=s.charAt(i); //temp variable
int otherEnd= length-i-1;
s.setCharAt(i,s.charAt(otherEnd));
s.setCharAt(otherEnd,current);
}
return s.toString();
}
}