输入字符串是 String sentence =“comp!ex,ex.amp!e”;
输出应该是 String output =“pmoc!xe,xe.pma!e”;
试过这个:
static void reverseEachWordOfString(String inputString)
{
String[] words = inputString.split("[\\s,!.]");
String reverseString = "";
for (int i = 0; i < words.length; i++)
{
String word = words[i];
String reverseWord = "";
for (int j = word.length()-1; j >= 0; j--)
{
reverseWord = reverseWord + word.charAt(j);
}
reverseString = reverseString + reverseWord + " ";
}
}
答案 0 :(得分:0)
以下示例迭代输入。它会停止,直到当前字符与regualr表达式匹配。表达式在代码中解释。可以通过应该分割输入的更多特殊字符来扩展它。
之间的子串将被反转并附加到输出字符串。如果这不符合要求,请编辑您的问题并提供更多信息。
public static void main(String[] args)
{
String reversed = reverse("comp!ex, ex.amp!e");
//
// Just to test...
//
String expectedOutput = "pmoc!xe, xe.pma!e";
if(expectedOutput.equals(reversed) == false)
{
throw new IllegalStateException(reversed);
}
}
public static String reverse(String input)
{
//
// Regular expression to match characters that should split the string.
// ! or . or , or 'a sequence of whitespace'
//
String specialCharExpression = "!|\\.|,| *";
StringBuilder result = new StringBuilder();
int fragmentStart = 0;
for(int i = 0; i < input.length(); i++)
{
String currentChar = input.charAt(i) + "";
if(currentChar.matches(specialCharExpression)
|| i == input.length() - 1)
{
result
.append(new StringBuilder(input.substring(fragmentStart, i))
.reverse().toString());
result.append(currentChar);
fragmentStart = i + 1;
}
}
return result.toString();
}
public static void main(String[] args)
{
String reversed = reverse("comp!ex, ex.amp!e");
//
// Just to test...
//
String expectedOutput = "pmoc!xe, xe.pma!e";
if(expectedOutput.equals(reversed) == false)
{
throw new IllegalStateException(reversed);
}
}
public static String reverse(String input)
{
//
// Regular expression to match characters that should split the string.
// ! or . or , or 'a sequence of whitespace'
//
String specialCharExpression = "!|\\.|,| *";
StringBuilder result = new StringBuilder();
int fragmentStart = 0;
for(int i = 0; i < input.length(); i++)
{
String currentChar = input.charAt(i) + "";
if(currentChar.matches(specialCharExpression)
|| i == input.length() - 1)
{
result
.append(new StringBuilder(input.substring(fragmentStart, i))
.reverse().toString());
result.append(currentChar);
fragmentStart = i + 1;
}
}
return result.toString();
}
答案 1 :(得分:0)
在下方找到您可以从中开始的代码段。代码中有附加说明,解释了所做的事情。
String sentence = "comp!ex, ex.amp!e";
String[] split = sentence.split("[\\s,!.]");
StringBuilder out = new StringBuilder(sentence.length());
StringBuilder tmp = new StringBuilder(sentence.length());
for (String s : split) {
// reset the length and reuse the tmp StringBuilder
tmp.setLength(0);
// append the part the the temporary StringBuilder
tmp.append(s);
// append the reversed part to the output StringBuilder
out.append(tmp.reverse());
// if the length of the input is longer then the output
// we need to add the separator char from the input
if (sentence.length() > out.length()) {
out.append(sentence.charAt(out.length()));
}
}
System.out.println("input : " + sentence);
System.out.println("reversed: " + out);
输出
input : comp!ex, ex.amp!e
reversed: pmoc!xe, xe.pma!e
答案 2 :(得分:0)
这是使用特殊字符反转String而不拆分字符串的另一种方法。
String
转换为char
数组。 char[]
。{/ li>上采用char[]
参数和循环
char[index]
是否为字母的方法。char[index]
不是字母表时存储起始索引和结束索引,并使用起始索引交换char[]
中的元素
结束指数。这是工作code:
public static boolean isAlphabet(char x) {
return ((x >= 'A' && x <= 'Z') || (x >= 'a' && x <= 'z'));
}
public static void reverse(char ch[]) {
int l = 0;
int startIndex = 0;
int endIndex = 0;
while (l < ch.length - 1) {
if (isAlphabet(ch[l])) {
l++;
} else {
endIndex = l - 1;
while (startIndex < endIndex){
char temp = ch[startIndex];
ch[startIndex] = ch[endIndex];
ch[endIndex] = temp;
endIndex--;
startIndex++;
}
l++;
startIndex = l;
}
}
}
public static void main(String[] args) throws java.lang.Exception {
String inputString = "comp!ex, ex.amp!e";
char[] ch = inputString.toCharArray();
reverse(ch);
String reverseString = new String(ch);
System.out.println(reverseString);
}
输入:comp!ex, ex.amp!e
输出:pmoc!xe, xe.pma!e
通过将String
与whitespace
分开,您可以使用相同的isAlphabet
和reverse
方法。
Code:
String inputString = "comp!ex, ex.amp!e";
String[] splitArray = inputString.split("\\s");
char[] ch1 = splitArray[0].toCharArray();
reverse(ch1);
char[] ch2 = splitArray[1].toCharArray();
reverse(ch2);
StringBuilder reverseString = new StringBuilder();
reverseString.append(ch1);
reverseString.append(" ");
reverseString.append(ch2);
System.out.println(reverseString.toString());
输出:
Input : comp!ex, ex.amp!e
Output: pmoc!ex, xe.pma!e
答案 3 :(得分:0)
使用堆栈的解决方案
还有一种方法isDelimiter()
,允许您添加任意数量的分隔符。
static String reverseEachWordOfString(String inputString) {
StringBuffer reversedString = new StringBuffer();
Stack<Character> stack = new Stack<Character>();
for(int i=0;i<inputString.length();i++){
// Get each character
char character = inputString.charAt(i);
if(isDelimiter(character)) { // If character is delimiter
popInto(stack, reversedString); // Then pop stack into output string
reversedString.append(character); // And append the delimiter
} else
stack.push(character); // Else push character onto the stack
}
popInto(stack, reversedString); // In the end pop stack into output string
return reversedString.toString();
}
private static void popInto(Stack<Character> stack, StringBuffer str) {
while(!stack.empty()) {
str.append(stack.pop());
}
}
private static boolean isDelimiter(char character) {
return character == ' ' || character == '.' || character == ',' || character == '!';
}