提问人:sriram 提问时间:6/17/2023 最后编辑:sriram 更新时间:6/19/2023 访问量:66
使用方法反转用户输入字符串
Reversing a user input string using methods
问:
这是一个问题,它从用户那里获取字符串输入并反转字符串。
例如: 输入:“我爱冰淇淋! 输出:“奶油!冰爱我”
源代码如下:
import java.util.*;
public class stringProblems {
static void reverse(char str[], int start, int end){
char temp;
while(start <= end){
temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
}
static char[] reverseWords(char[] s){
int start = 0;
for(int end = 0; end < s.length; end++){
if(s[end] == ' '){
reverse(s, start, end);
start = end + 1;
}
}
reverse(s, start, s.length -1);
reverse(s, 0, s.length-1);
return s;
}
public static void main(String[] args) {
System.out.println("enter the string to be reversed");
Scanner ss = new Scanner(System.in);
String s = ss.nextLine();
char[] p = reverseWords(s.toCharArray());
System.out.println(p);
}
}
我怀疑的是 reverseWords 方法中的 for 循环会迭代,直到它找到一个空格。我给出的输入是“我喜欢冰淇淋!因此,在第二次迭代时,if(s[end] == ' ') 语句中的条件变为 true,并调用了 reverse 方法。反向方法中的 while(start <= end) 循环在第一次迭代时执行,因为 start 的值为 0,end 的值为 1。最后,由于 start++,start 的值递增为 1,end 的值递减为 0;和结束--;在下一次迭代中,条件失败,它跳转到 reverseWords 方法内的 for(int end = 0; end <= s.length; end++) 循环,现在 end 的值以 0 或 2 开头,就像之前 1 一样,当 “ ” 空格位于 “I love ice cream!” 的 1 索引处时?
提示:理解上面的源代码才能理解我的问题。
答:
更改 “reverseWords” 的方法,如下所示
static char[] reverseWords(char[] s){
int start = 0;
reverse(s, start, s.length -1);
return s;
}
请尝试以下操作来颠倒单词顺序
public static String reverseString(String input) {
String[] array = input.split(" ");
String output="";
for(String str:array) {
output = str+" "+output;
}
return output;
}
评论
如果我正确理解了这个问题(最后一段有点啰嗦),那么您是在问两个不同的方法中存在相互调用的变量,以及这如何影响这些值。start
end
简短的回答是,它们根本不相互影响。在一个函数中声明的变量与另一个函数中的同名变量完全无关,其中包括函数参数。
对 中完成的更改对 不可见。您可以在之前和之后打印,您将看到相同的值。如果之前是 1,那么之后就是 1。end
reverse
reverseWords
end
reverse(s, start, end);
end
请注意,当您拥有容器和对象时,对这些容器和对象中的值所做的更改将对所有人可见。这是因为变量本身就是一个引用,如果你把这个引用交给另一个函数,它们都会指向相同的底层数据结构。
至于你的错误,问题是在 和 inclusive 之间交换所有字母,也就是说,它交换了 ,然后 ,等等。这是一个问题,因为调用它时,所以第一个字母将与空格交换,而不是最后一个字母。reverse
start
end
s[start] <-> s[end]
s[start+1] <-> s[end-1]
reverseWords
s[end] == ' '
要修复它,只需调用结尾向后移动一点:reverse
static char[] reverseWords(char[] s){
int start = 0;
for(int end = 0; end < s.length; end++){
if(s[end] == ' '){
// Changed here to end-1.
reverse(s, start, end-1);
start = end + 1;
}
}
reverse(s, start, s.length -1);
reverse(s, 0, s.length-1);
return s;
}
enter the string to be reversed
I like ice cream!
cream! ice like I
评论
有很多要解释的。
通过将相关值打印到标准输出,可以更轻松地可视化数据流。
我已经从单独的方法中实现了一些输出。
static void reverse(char str[], int start, int end){
char temp;
while(start <= end){
System.out.printf(" reverse,%n start = %d, end = %d%n", start, end);
temp = str[start];
str[start] = str[end];
str[end] = temp;
System.out.printf(" temp assigned '%s'%n", str[start]);
System.out.printf(" str[start] assigned '%s'%n", str[end]);
System.out.printf(" str[end] assigned '%s'%n", temp);
start++;
end--;
}
}
static char[] reverseWords(char[] s){
int start = 0;
for(int end = 0; end < s.length; end++){
System.out.printf("reverseWords, s[end] = '%s'%n", s[end]);
if(s[end] == ' '){
reverse(s, start, end);
start = end + 1;
}
}
reverse(s, start, s.length -1);
reverse(s, 0, s.length-1);
return s;
}
public static void main(String[] args) {
System.out.println("enter the string to be reversed");
Scanner ss = new Scanner(System.in);
String s = ss.nextLine();
char[] p = reverseWords(s.toCharArray());
System.out.println(p);
}
输出
enter the string to be reversed
I like ice cream!
reverseWords, s[end] = 'I'
reverseWords, s[end] = ' '
reverse,
start = 0, end = 1
temp assigned ' '
str[start] assigned 'I'
str[end] assigned 'I'
reverseWords, s[end] = 'l'
reverseWords, s[end] = 'i'
reverseWords, s[end] = 'k'
reverseWords, s[end] = 'e'
reverseWords, s[end] = ' '
reverse,
start = 2, end = 6
temp assigned ' '
str[start] assigned 'l'
str[end] assigned 'l'
reverse,
start = 3, end = 5
temp assigned 'e'
str[start] assigned 'i'
str[end] assigned 'i'
reverse,
start = 4, end = 4
temp assigned 'k'
str[start] assigned 'k'
str[end] assigned 'k'
reverseWords, s[end] = 'i'
reverseWords, s[end] = 'c'
reverseWords, s[end] = 'e'
reverseWords, s[end] = ' '
reverse,
start = 7, end = 10
temp assigned ' '
str[start] assigned 'i'
str[end] assigned 'i'
reverse,
start = 8, end = 9
temp assigned 'e'
str[start] assigned 'c'
str[end] assigned 'c'
reverseWords, s[end] = 'c'
reverseWords, s[end] = 'r'
reverseWords, s[end] = 'e'
reverseWords, s[end] = 'a'
reverseWords, s[end] = 'm'
reverseWords, s[end] = '!'
reverse,
start = 11, end = 16
temp assigned '!'
str[start] assigned 'c'
str[end] assigned 'c'
reverse,
start = 12, end = 15
temp assigned 'm'
str[start] assigned 'r'
str[end] assigned 'r'
reverse,
start = 13, end = 14
temp assigned 'a'
str[start] assigned 'e'
str[end] assigned 'e'
reverse,
start = 0, end = 16
temp assigned 'c'
str[start] assigned ' '
str[end] assigned ' '
reverse,
start = 1, end = 15
temp assigned 'r'
str[start] assigned 'I'
str[end] assigned 'I'
reverse,
start = 2, end = 14
temp assigned 'e'
str[start] assigned ' '
str[end] assigned ' '
reverse,
start = 3, end = 13
temp assigned 'a'
str[start] assigned 'e'
str[end] assigned 'e'
reverse,
start = 4, end = 12
temp assigned 'm'
str[start] assigned 'k'
str[end] assigned 'k'
reverse,
start = 5, end = 11
temp assigned '!'
str[start] assigned 'i'
str[end] assigned 'i'
reverse,
start = 6, end = 10
temp assigned 'i'
str[start] assigned 'l'
str[end] assigned 'l'
reverse,
start = 7, end = 9
temp assigned 'c'
str[start] assigned ' '
str[end] assigned ' '
reverse,
start = 8, end = 8
temp assigned 'e'
str[start] assigned 'e'
str[end] assigned 'e'
cream!ice like I
评论