public String reverseWords_andCase(String sentence, int case_option) {
// Split the sentence into words based on spaces
String[] words = [Link](" ");
StringBuilder result = new StringBuilder();
// Process each word in the sentence
for (int i = 0; i < [Link]; i++) {
String originalWord = words[i];
String reversedWord = reverseWord(originalWord);
// Modify the word based on the case_option
String modifiedWord;
if (case_option == 0) { // Normal reversal
modifiedWord = reversedWord;
} else if (case_option == 1) { // Retain case positions
modifiedWord = retainCasePositions(originalWord, reversedWord);
} else {
throw new UnsupportedOperationException("Invalid case_option: " + case_option);
}
// Append the modified word to the result
[Link](modifiedWord);
// Add a space if it's not the last word
if (i < [Link] - 1) {
[Link](" ");
}
}
// Assign the result to the output variable
output1 = [Link]();
return output1; // Return the final reversed string
}
// Helper function to reverse a word
private String reverseWord(String word) {
return new StringBuilder(word).reverse().toString();
}
// Helper function to retain original case positions
private String retainCasePositions(String original, String reversed) {
char[] result = [Link]();
for (int i = 0; i < [Link](); i++) {
if ([Link]([Link](i))) {
result[i] = [Link](result[i]);
} else if ([Link]([Link](i))) {
result[i] = [Link](result[i]);
}
}
return new String(result);
}