-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReverseOnlyLetters.java
36 lines (27 loc) · 979 Bytes
/
ReverseOnlyLetters.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
class ReverseOnlyLetters{
public static void main(String[] args) {
String s = "abc!=detchg-?hy&*yoo";
System.out.println("THE REVERSED LETTERS ARE: "+ reverseOnlyLetters(s));
}
/*The idea is to take
1. Save only letters into StringBuilder;
2. Reverse it;
3. Insert the non-letters like !=& whenever they are found.
*/
private static String reverseOnlyLetters(String s){
StringBuffer sb = new StringBuffer();
for(char c : s.toCharArray()){
if(Character.isLetter(c)) //if its a letter then append it to the SB
sb.append(c); //eg abcdetchghyyoo in the sb
}
sb.reverse(); //Since we want reverse do the rev
for (int i=0; i<s.length(); ++i) { //take a loop from start to end
char c = s.charAt(i); //get the character
if(!Character.isLetter(c)){ //if its not the letter
sb.insert(i, c); //Insert at ith place the non letter we found
System.out.println(sb.toString());
}
}
return sb.toString();
}
}