在java中反转字符串数组
我需要做一个程序,采取用户输入的句子,并用适当的格式和标点符号将其反转 敏捷的棕色狐狸跳过了懒狗 结果:狗懒洋洋的跳过狐狸棕色的快步 我已经看到了解决这个问题的方法,我可以通过一次询问用户一个单词来得到正确的答案。但是,我们被特别要求只向用户询问句子,然后由程序完成其余部分。因此,程序必须确定数组的大小,并将字符串中的每个字赋给数组中的一个值 到目前为止,这就是我所拥有的,但是我认为我需要使用stringBuffer,但我不知道如何实现它在java中反转字符串数组,java,arrays,Java,Arrays,我需要做一个程序,采取用户输入的句子,并用适当的格式和标点符号将其反转 敏捷的棕色狐狸跳过了懒狗 结果:狗懒洋洋的跳过狐狸棕色的快步 我已经看到了解决这个问题的方法,我可以通过一次询问用户一个单词来得到正确的答案。但是,我们被特别要求只向用户询问句子,然后由程序完成其余部分。因此,程序必须确定数组的大小,并将字符串中的每个字赋给数组中的一个值 到目前为止,这就是我所拥有的,但是我认为我需要使用stringBuffer,但我不知道如何实现它 public class ReverseSentence
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 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();
}
您可以这样尝试:
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();
}
您有两个明显的问题: 改判 将句子大写 让我们先做第一部分:
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 String capitalise(final String name) {
return name.substring(0, 1).toUpperCase() + name.substring(1).toLowerCase();
}
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();
}
我们只需要在第一个单词上使用这个方法:
public static String capitalise(final String name) {
return name.substring(0, 1).toUpperCase() + name.substring(1).toLowerCase();
}
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();
}
再次检查:
狗懒洋洋的跳过狐狸棕色的快步
您有两个明显的问题: 改判 将句子大写 让我们先做第一部分:
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 String capitalise(final String name) {
return name.substring(0, 1).toUpperCase() + name.substring(1).toLowerCase();
}
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();
}
我们只需要在第一个单词上使用这个方法:
public static String capitalise(final String name) {
return name.substring(0, 1).toUpperCase() + name.substring(1).toLowerCase();
}
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();
}
再次检查:
狗懒洋洋的跳过狐狸棕色的快步
使用Apache Commons StringUtils
使用Apache Commons StringUtils
试试这个。此代码将使用句号给出所有格式的输出。。仔细阅读评论
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(".");
}
试试这个。此代码将使用句号给出所有格式的输出。。仔细阅读评论
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返回什么?一个单词还是整个句子?IO.readString返回什么?一个词还是整个句子?@Bubletan:-我想OP可以加上那个词。无论如何,要大写字符串的第一个单词,只需添加这一行……字符串输出=input.substring0,1.toUpperCase+input.substring1@我想OP可以加上那个。无论如何,要大写字符串的第一个单词,只需添加这一行……字符串输出=input.substring0,1.toUpperCase+input.substring1;实际上是在问另外两个我如何实施资本化。这里的一切看起来都很完美,效果也很好。我们将通过调试程序运行几次以完全理解它。非常感谢。实际上是在问另外两个我如何实施资本化。这里的一切看起来都很完美,效果也很好。我们将通过调试程序运行几次以完全理解它。非常感谢你。