我已经编写了一个代码来反转给定字符串中元音的顺序,但是我还需要保持字符串中元音大写的原始位置。例如,输入:Java Is Fun
输出:Juvi As Fan
而不是JuvI as Fan
。
到目前为止,这是我的代码,目前输出JuvI as Fan
:
static boolean isVowel(char c)
{
return (c == 'a' || c == 'A' || c == 'e'
|| c == 'E' || c == 'i' || c == 'I'
|| c == 'o' || c == 'O' || c == 'u'
|| c == 'U');
public static String reverseVowels(String text)
{
int index = 0;
char[] str = text.toCharArray();
String vowel = "";
for (int i = 0; i < str.length; i++)
{
if (isVowel(str[i]))
{
index++;
vowel += str[i];
}
}
for (int i = 0; i < str.length; i++)
{
if (isVowel(str[i]))
{
str[i] = vowel.charAt(--index);
}
}
return String.valueOf(str);
答案 0 :(得分:1)
我会记录所有大写字母(如果字符串中的大写字母比小写字母大,则记录小写字母)。
List<Integer> capitalLetters = new ArrayList<>(); // Positions of capital letters.
for (int i = 0; i < text.length(); i++) {
if (Character.isUpperCase(text.charAt(i))) {
capitalLetters.add(i);
}
}
text = text.toLowerCase();
int index = 0;
char[] str = text.toCharArray();
String vowel = "";
for (int i = 0; i < str.length; i++) {
if (isVowel(str[i])) {
index++;
vowel += str[i];
}
}
for (int i = 0; i < str.length; i++) {
if (isVowel(str[i])) {
str[i] = vowel.charAt(--index);
}
}
// Recapitalise.
for (int pos : capitalLetters) {
str[pos] = Character.toUpperCase(str[pos]);
}
return str;
我还将您的元音检查功能更改为:
c = Character.toLowerCase(c);
return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
为避免检查大写字母
答案 1 :(得分:0)
我会从左右搜索String
,寻找下一个元音字符,然后交换两个,同时保留原始大小写。有点冗长,但我喜欢构建更通用的解决方案,以便可在不同情况下重用。
也许对您来说有价值。
public class ReverseVowels {
private static final char[] VOWELS = new char[] { 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U' };
public static void main(String[] args) {
final String input = "Java Is Fun";
final String expected = "Juvi As Fan";
final String actual = reverseVowels(input);
if (actual.equals(expected)) {
System.out.println("Awesome! It worked!");
}
}
/**
* Reverses the vowels within the given string. Keeps the original character's case.
*
* @param s the string to reverse the vowels in
* @return the manipulated string
*/
private static String reverseVowels(String s) {
char[] chars = s.toCharArray();
int l = -1;
int r = chars.length;
do {
l = indexOfAny(s, VOWELS, l + 1);
r = lastIndexOfAny(s, VOWELS, r - 1);
if (l <= r && l > -1) {
char charLeft = chars[l];
char charRight = chars[r];
chars[l] = Character.isUpperCase(charLeft) ? Character.toUpperCase(charRight) : Character.toLowerCase(charRight);
chars[r] = Character.isUpperCase(charRight) ? Character.toUpperCase(charLeft) : Character.toLowerCase(charLeft);
}
} while (l > -1 && r > -1 && r > l);
return new String(chars);
}
/**
* Finds the next position of any given char in the string, starting at the given position.
*
* @param s the string to search
* @param chars the chars to find
* @param startPos the start position
* @return the index of the next char or -1 if char could not be found
*/
public static int indexOfAny(String s, char[] chars, int startPos) {
startPos = Math.max(0, startPos);
for (int i = startPos; i < s.length(); i++) {
char c = s.charAt(i);
if (containsChar(chars, c)) {
return i;
}
}
return -1;
}
/**
* Finds the next position of any given char in the string, looking backwards, starting at the given position.
*
* @param s the string to search
* @param chars the chars to find
* @param startPos the start position
* @return the index of the next char or -1 if char could not be found
*/
public static int lastIndexOfAny(String s, char[] chars, int startPos) {
startPos = Math.min(s.length() - 1, startPos);
for (int i = startPos; i >= 0; i--) {
char c = s.charAt(i);
if (containsChar(chars, c)) {
return i;
}
}
return -1;
}
/**
* Returns true, if the char array chars contains the given char c.
*
* @param chars the char array to search in
* @param c the char to search for
* @return true if found, false otherwise
*/
public static boolean containsChar(char[] chars, char c) {
for (char c2 : chars) {
if (c == c2) {
return true;
}
}
return false;
}
}