颠倒java中的字符串数组
我需要制作一个程序,它接受用户输入的句子,并用正确的格式和标点符号将其颠倒。颠倒java中的字符串数组
例如:快速的棕色狐狸跳过懒惰的狗。
结果:“狗懒得跳过狐狸棕色快。”
我已经看到了解决方案,我可以通过每次询问用户1个单词来得到正确答案。然而,我们特别要求仅向用户询问句子,然后该程序完成剩下的工作。所以程序必须确定数组的大小,并将字符串中的每个单词分配给数组中的一个值(我猜?)。
到目前为止,这是我的,但我认为我需要使用stringBuffer,但我不知道如何实现这一点。
public class ReverseSentence {
public static void main(String[] args) {
String[] sentence = new String[]{IO.readString()};
for(int counter = 0; counter < sentence.length; counter++){
System.out.println("Enter Sentence"+(counter+1));
sentence[counter] = IO.readString();
}
System.out.println("The Reversed Sentence is:");
for(int counter = sentence.length - 1; counter >= 0; counter--){
System.out.print(sentence[counter]);
}
}
}
这不是一个家庭作业,只为即将到来的考试实践中的一些问题,所以一个完整的解决方案是好的,但如果可能的话,带注释行,所以我可以看到你是如何做到的。
你有两个不同的问题:
- 扭转了一句
- 资本句子
让我们做的第一部分拳头:
public static String reverseSentence(final String sentence) {
final Pattern pattern = Pattern.compile("[^A-Za-z']+");
final List<String> words = pattern.splitAsStream(sentence)
.map(String::toLowerCase)
.collect(toList());
final StringBuilder reversed = new StringBuilder();
final ListIterator<String> i = words.listIterator(words.size());
reversed.append(i.previous());
while (i.hasPrevious()) {
reversed
.append(" ")
.append(i.previous());
}
reversed.append(".");
return reversed.toString();
}
仔细检查代码:
public static void main(String[] args) throws Exception {
System.out.println(reverseSentence("The quick brown fox jumps over the lazy dog"));
}
狗懒过狐狸跳快速褐色的。
好了,现在我们需要大写第一个字:
我们只需要使用此方法的第一个字:
public static String reverseSentence(final String sentence) {
final Pattern pattern = Pattern.compile("[^A-Za-z']+");
final List<String> words = pattern.splitAsStream(sentence)
.map(String::toLowerCase)
.collect(toList());
final StringBuilder reversed = new StringBuilder();
final ListIterator<String> i = words.listIterator(words.size());
reversed.append(capitalise(i.previous()));
while (i.hasPrevious()) {
reversed
.append(" ")
.append(i.previous());
}
reversed.append(".");
return reversed.toString();
}
检查一遍:
狗懒过狐狸棕快速的。
字面上是在问另外两个人如何实施大写。这里的一切看起来都很完美,而且效果很棒要通过调试器运行几次才能完全理解它。非常感谢。 – Blake
你可以尝试这样的:
public static String reverseString(String input) {
//from input to this method
// split input with space and store words
// in a collection if input is not empty
Deque<String> words = new ArrayDeque<>();
for (String word: input.split(" ")) {
if (!word.isEmpty()) {
words.addFirst(word);
}
}
//now build output in reverse order of
// addition to collection if input is not empty
StringBuilder result = new StringBuilder();
while (!words.isEmpty()) {
result.append(words.removeFirst());
if (!words.isEmpty()) {
result.append(" ");
}
}
return result.toString();
}
这不会正确格式化该句子。 – Bubletan
@Bubletan: - 我猜OP可以添加那个。无论如何要大写的字符串的第一个字,你只需要添加这一行....'字符串输出= input.substring(0,1).toUpperCase()+ input.substring(1);' –
使用Apache Commons StringUtils。
https://commons.apache.org/proper/commons-lang/
String result = StringUtils.capitalize(StringUtils.reverse(StringUtils.uncapitalize(basicString.substring(0,basicString.length()-1)))) + ".";
试试这个。此代码将使用句号(。)输出所有格式的输出。并仔细阅读评论。
Scanner inp = new Scanner(System.in);
String s1 = inp.nextLine();
String s2[] = s1.split(" ");
boolean full_stop = false;
// Printing first character of last string in upper case
System.out.print(Character.toUpperCase(s2[s2.length - 1].charAt(0)));
// Printing rest of the character of last string
if (s2[s2.length - 1].contains(".")) {// checking that (.) is exists then print without (.)
System.out.print(s2[s2.length - 1].substring(1,s2[s2.length - 1].length() - 1) + " ");
full_stop = true;
} else {
System.out.print(s2[s2.length - 1].substring(1, s2[s2.length - 1].length()) + " ");
}
for (int i = s2.length - 2; i >= 0; i--) {
if (i > 0) {
System.out.print(s2[i] + " ");
} else {
System.out.print(Character.toLowerCase(s2[i].charAt(0)));//converting first string character to lower case
System.out.print(s2[i].substring(1,s2[i].length()));// last string must not have space after that
}
}
if (full_stop) {// printing (.) if exists
System.out.println(".");
}
什么是IO。readString()'return?一个词还是整个句子? – Bubletan