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

5q4ezhmt  于 2023-06-20  发布在  Java
关注(0)|答案(3)|浏览(105)

这里有一个问题,从用户那里得到一个字符串输入,然后反转字符串。
例如:输入:"我爱冰淇淋!”输出:“奶油!冰爱我”
下面是源代码:

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; and end--;在下一次迭代中条件失败,它跳转到for(int end = 0; end <= s. length; end ++)在reverseWords方法中循环,现在end的值从0或2开始,因为它是1,因为“I love ice cream!"?
提示:看懂上面的源代码就能明白我的问题。

sy5wg1nm

sy5wg1nm1#

如果我理解正确的话(最后一段有点冗长),你问的是在两个不同的方法中有startend变量相互调用,以及这如何影响这些值。
简短的回答是,它们根本不会相互影响。在一个函数中声明的变量与另一个函数中同名的变量完全无关,这包括函数参数。
reverse中对end所做的更改对reverseWords不可见。您可以在reverse(s, start, end);之前和之后打印end,您将看到相同的值。如果end之前是1,那么之后就是1。
请注意,当你有容器和对象时,对这些容器和对象内部的值所做的更改将对所有人可见。这是因为变量本身就是一个引用,如果你把这个引用交给另一个函数,它们都会指向同一个底层数据结构。
至于你的bug,问题是reverse交换了startend之间的所有字母,也就是说,它交换了s[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
ruarlubt

ruarlubt2#

有很多事情要解释。
通过将相关值打印到标准输出,可以更容易地可视化数据流。
我已经实现了一些输出,从单独的方法中。

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
9avjhtql

9avjhtql3#

//修改“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;
}

相关问题