package com.ofs;
public class ReverseWordsInString{
public static void main(String[] args) {
String str = "welcome to the new world and how are you feeling ?";
// Get the Java runtime
Runtime runtime = Runtime.getRuntime();
// Run the garbage collector
runtime.gc();
// Calculate the used memory
long firstUsageMemory = runtime.totalMemory() - runtime.freeMemory();
System.out.println("Used memory in bytes: " + firstUsageMemory);
System.out.println(str);
str = new StringBuffer(str).reverse().toString();
int count = 0;
int preValue = 0;
int lastspaceIndexVal = str.lastIndexOf(" ");
int strLen = str.length();
for (int i = 0; i < strLen - 1; i++) {
if (Character.isWhitespace(str.charAt(i))) {
if (i - preValue == 1 && count == 0) {
str = str.substring(0, preValue) + str.charAt(i - 1)
+ str.substring(i, strLen);
preValue = i;
count++;
} else if (i - preValue == 2 && count == 0) {
str = str.substring(0, preValue) + str.charAt(i - 1)
+ str.charAt(i - 2) + str.substring(i, strLen);
preValue = i;
count++;
} else if (i - preValue == 3 && count == 0) {
str = str.substring(0, preValue) + str.charAt(i - 1)
+ str.charAt(i - 2) + str.charAt(i - 3)
+ str.substring(i, strLen);
preValue = i;
count++;
} else if (i - preValue == 4 && count == 0) {
str = str.substring(0, preValue) + str.charAt(i - 1)
+ str.charAt(i - 2) + str.charAt(i - 3)
+ str.charAt(i - 4) + str.substring(i, strLen);
preValue = i;
count++;
} else if (i - preValue == 5 && count == 0) {
str = str.substring(0, preValue) + str.charAt(i - 1)
+ str.substring(i - 2, i - 1) + str.charAt(i - 3)
+ str.charAt(i - 3) + str.charAt(i - 5)
+ str.substring(i, strLen);
preValue = i;
count++;
} else if (i - preValue == 6 && count == 0) {
str = str.substring(0, preValue) + str.charAt(i - 1)
+ str.charAt(i - 2) + str.charAt(i - 3)
+ str.charAt(i - 4) + str.charAt(i - 5)
+ str.charAt(i - 6) + str.substring(i, strLen);
preValue = i;
count++;
} else if (i - preValue == 7 && count == 0) {
str = str.substring(0, preValue) + str.charAt(i - 1)
+ str.charAt(i - 2) + str.charAt(i - 3)
+ str.charAt(i - 4) + str.charAt(i - 5)
+ str.charAt(i - 6) + str.charAt(i - 7)
+ str.substring(i, strLen);
preValue = i;
count++;
} else if (i - preValue == 8 && count == 0) {
str = str.substring(0, preValue) + str.charAt(i - 1)
+ str.charAt(i - 2) + str.charAt(i - 3)
+ str.charAt(i - 4) + str.charAt(i - 5)
+ str.charAt(i - 6) + str.charAt(i - 7)
+ str.charAt(i - 8) + str.substring(i, strLen);
preValue = i;
count++;
} else if (i - preValue == 2 && count != 0) {
str = str.substring(0, preValue) + str.charAt(i - 1)
+ str.substring(i, strLen);
preValue = i;
} else if (i - preValue == 3 && count != 0) {
str = str.substring(0, preValue + 1) + str.charAt(i - 1)
+ str.charAt(i - 2) + str.substring(i, strLen);
preValue = i;
} else if (i - preValue == 4 && count != 0) {
str = str.substring(0, preValue + 1) + str.charAt(i - 1)
+ str.charAt(i - 2) + str.charAt(i - 3)
+ str.substring(i, strLen);
preValue = i;
} else if (i - preValue == 5 && count != 0) {
str = str.substring(0, preValue + 1) + str.charAt(i - 1)
+ str.charAt(i - 2) + str.charAt(i - 3)
+ str.charAt(i - 4) + str.substring(i, strLen);
preValue = i;
count++;
} else if (i - preValue == 6 && count != 0) {
str = str.substring(0, preValue + 1) + str.charAt(i - 1)
+ str.charAt(i - 2) + str.charAt(i - 3)
+ str.charAt(i - 4) + str.charAt(i - 5)
+ str.substring(i, strLen);
preValue = i;
count++;
} else if (i - preValue == 7 && count != 0) {
str = str.substring(0, preValue + 1) + str.charAt(i - 1)
+ str.charAt(i - 2) + str.charAt(i - 3)
+ str.charAt(i - 4) + str.charAt(i - 5)
+ str.charAt(i - 6) + str.substring(i, strLen);
preValue = i;
count++;
} else if (i - preValue == 8 && count != 0) {
str = str.substring(0, preValue + 1) + str.charAt(i - 1)
+ str.charAt(i - 2) + str.charAt(i - 3)
+ str.charAt(i - 4) + str.charAt(i - 5)
+ str.charAt(i - 6) + str.charAt(i - 7)
+ str.substring(i, strLen);
preValue = i;
count++;
}
if (lastspaceIndexVal == preValue) {
if (strLen - lastspaceIndexVal == 2 && count != 0) {
str = str.substring(0, preValue + 1)
+ str.charAt(strLen - 1);
preValue = i;
} else if (strLen - lastspaceIndexVal == 3 && count != 0) {
str = str.substring(0, preValue + 1)
+ str.charAt(strLen - 1)
+ str.charAt(strLen - 2);
preValue = i;
} else if (strLen - lastspaceIndexVal == 4 && count != 0) {
str = str.substring(0, preValue + 1)
+ str.charAt(strLen - 1)
+ str.charAt(strLen - 2)
+ str.charAt(strLen - 3);
preValue = i;
count++;
} else if (strLen - lastspaceIndexVal == 5 && count != 0) {
str = str.substring(0, preValue + 1)
+ str.charAt(strLen - 1)
+ str.charAt(strLen - 2)
+ str.charAt(strLen - 3)
+ str.charAt(strLen - 4);
preValue = i;
} else if (strLen - lastspaceIndexVal == 6 && count != 0) {
str = str.substring(0, preValue + 1)
+ str.charAt(strLen - 1)
+ str.charAt(strLen - 2)
+ str.charAt(strLen - 3)
+ str.charAt(strLen - 4)
+ str.charAt(strLen - 5);
preValue = i;
count++;
} else if (strLen - lastspaceIndexVal == 7 && count != 0) {
str = str.substring(0, preValue + 1)
+ str.charAt(strLen - 1)
+ str.charAt(strLen - 2)
+ str.charAt(strLen - 3)
+ str.charAt(strLen - 4)
+ str.charAt(strLen - 5)
+ str.charAt(strLen - 6);
preValue = i;
} else if (strLen - lastspaceIndexVal == 8 && count != 0) {
str = str.substring(0, preValue + 1)
+ str.charAt(strLen - 1)
+ str.charAt(strLen - 2)
+ str.charAt(strLen - 3)
+ str.charAt(strLen - 4)
+ str.charAt(strLen - 5)
+ str.charAt(strLen - 6)
+ str.charAt(strLen - 7);
preValue = i;
}
}
}
}
runtime.gc();
// Calculate the used memory
long SecondaryUsageMemory = runtime.totalMemory()
- runtime.freeMemory();
System.out.println("Used memory in bytes: " + SecondaryUsageMemory);
System.out.println(str);
}
}