-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReverseEvenWordsInAStringUsingStringBuffer.java
42 lines (33 loc) · 1.32 KB
/
ReverseEvenWordsInAStringUsingStringBuffer.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
38
39
40
41
42
package powerJavaLearning;
public class ReverseEvenWordsInAStringUsingStringBuffer {
/*Write a java program to do the following.
* Input: "When the world realise its own mistakes, corona will dissolve automatically"
* output: "When eht world esilaer its nwo mistakes, anoroc will evlossid automatically"
* */
public static void main(String[] args) {
String input = "When the world realise its own mistakes, corona will dissolve automatically";
String[] inputSplit= input.split(" ");
//String Reversal Using append function
System.out.println("Reversal using Append " );
StringBuffer tempString01 = new StringBuffer();
for (int i = 0; i < inputSplit.length; i++) {
if ((i%2) != 0) {
tempString01.append(new StringBuffer(inputSplit[i]).reverse()).append(" ");
} else {
tempString01.append(new StringBuffer(inputSplit[i])).append(" ");
}
}
System.out.println(tempString01);
//String Reversal Using reverse function
System.out.println("Reversal using reverse " );
for (int i = 0; i < inputSplit.length; i++) {
if ((i%2) != 0) {
StringBuffer tempString02 = new StringBuffer(inputSplit[i]);
inputSplit[i] = tempString02.reverse().toString();
}
}
for (int i = 0; i < inputSplit.length; i++) {
System.out.print(inputSplit[i] + " ");
}
}
}