使用方法反转用户输入字符串

Reversing a user input string using methods

提问人:sriram 提问时间:6/17/2023 最后编辑:sriram 更新时间:6/19/2023 访问量:66

问:

这是一个问题,它从用户那里获取字符串输入并反转字符串。

例如: 输入:“我爱冰淇淋! 输出:“奶油!冰爱我”

源代码如下:

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 索引处时?

提示:理解上面的源代码才能理解我的问题。

java 字符串 for-loop while-loop char

评论

0赞 sriram 6/17/2023
@WJS当然!但我想知道在这种特殊情况下具体会发生什么?
0赞 Reilas 6/18/2023
是否存在错误,或者您是否要求解释代码的工作原理?
0赞 sriram 6/19/2023
@Reilas 我想了解代码在上述情况下是如何工作的。

答:

-1赞 Mohanraj Rathinakumar 6/17/2023 #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;
}

评论

1赞 BoppreH 6/17/2023
这将反转整个字符串,打印“!maerc eci ekil I”。OP 希望单词的顺序颠倒过来,例如“奶油!冰爱我”。
1赞 BoppreH 6/17/2023 #2

如果我正确理解了这个问题(最后一段有点啰嗦),那么您是在问两个不同的方法中存在相互调用的变量,以及这如何影响这些值。startend

简短的回答是,它们根本不相互影响。在一个函数中声明的变量与另一个函数中的同名变量完全无关,其中包括函数参数。

对 中完成的更改对 不可见。您可以在之前和之后打印,您将看到相同的值。如果之前是 1,那么之后就是 1。endreversereverseWordsendreverse(s, start, end);end

请注意,当您拥有容器和对象时,对这些容器和对象中的值所做的更改将对所有人可见。这是因为变量本身就是一个引用,如果你把这个引用交给另一个函数,它们都会指向相同的底层数据结构。


至于你的错误,问题是在 和 inclusive 之间交换所有字母,也就是说,它交换了 ,然后 ,等等。这是一个问题,因为调用它时,所以第一个字母将与空格交换,而不是最后一个字母。reversestartends[start] <-> s[end]s[start+1] <-> s[end-1]reverseWordss[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

评论

0赞 sriram 6/18/2023
@BpppreH有道理,谢谢!
0赞 BoppreH 6/18/2023
嗨,@sriram如果这个或任何答案解决了您的问题,请考虑通过单击复选标记来接受它。这向更广泛的社区表明您已经找到了解决方案,并为回答者和您自己赢得了一些声誉。没有义务这样做。
0赞 sriram 6/18/2023
嘿@BoppreH在获取 String 类型的输入后,该值是否在运行时或编译时存储在字符串常量池或堆内存中?即使将 String 作为参数传递给 reverseWords 方法和 reverse 方法,或者它不会存储在内存中??
0赞 sriram 6/18/2023
就像如果它存储在这个内存中的任何一个中,它在什么时间发生,无论是运行时还是编译时?
0赞 BoppreH 6/18/2023
@sriram 因为它是用扫描程序读取的,所以它存储在堆中。函数之间共享相同的字符串,不进行任何复制。试着在末尾打印“s”,你会看到它也被颠倒了。
0赞 Reilas 6/19/2023 #3

有很多要解释的。

通过将相关值打印到标准输出,可以更轻松地可视化数据流。

我已经从单独的方法中实现了一些输出。

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