简体   繁体   English

在不使用Java中库函数的情况下反转字符串

[英]Reverse a string without use of library function in java

I am new to programming I need to reverse a string without using the library function. 我是编程新手,我需要在不使用库函数的情况下反转字符串。

I am able to reverse but as expected. 我能够扭转,但正如预期的那样。

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
String rev = "";
ArrayList<Integer> list = new ArrayList<Integer>();
ArrayList<String> splitResult = new ArrayList<String>();

for (int i = 0; i < s.length(); i++)
    if (s.charAt(i) == ' ')
        list.add(i);
list.add(0, 0);
list.add(list.size(), s.length());
String[] words = new String[list.size()];

for (int j = 0; j <= words.length - 2; j++)
    splitResult.add(s.substring(list.get(j), list.get(j + 1)).trim());

System.out.println(splitResult);
String[] str = new String[splitResult.size()];
str = splitResult.toArray(str);

for (int i = 0; i < str.length; i++) {
    if (i == str.length - 1) {
        rev = str[i] + rev;
    } else {
        rev = " " + str[i] + rev;
    }
}

System.out.println(rev);

Expected: Input: i am coder output: redoc ma i 预期:输入:我是编码器输出:redoc ma i

actual input: i am coder output: coder am i 实际输入:我是编码器输出:我是编码器

You can just provide an empty result variable, iterate the characters of the given String by using an enhanced for -loop (also known as for -each loop) setting every character to index 0 of the result variable by just concatenating the character to the result variable like this: 您可以提供一个空的结果变量,使用增强的for循环(也称为for -each循环)来迭代给定String的字符,只需将字符与结果连接即可将每个字符设置为结果变量的索引0像这样的变量:

public static void main(String[] args) {
    // input
    String s = "I am coder";
    // result variable for reverse input
    String reverseS = "";

    // go through every single character of the input
    for (char c : s.toCharArray()) {
        // and concatenate it and the result variable
        reverseS = c + reverseS;
    }

    // then print the result
    System.out.println(reverseS);

}

You can of course do that in a slightly different way using a classic for -loop and the length of the input, see this example: 当然,您可以使用经典for -loop和输入的长度,以稍微不同的方式执行此操作,请参见以下示例:

public static void main(String[] args) {
    String s = "I am coder";

    String reverseS = "";

    for (int i = 0; i < s.length(); i++) {
        reverseS = s.charAt(i) + reverseS;
    }

    System.out.println(reverseS);

}
    String s = "I am coder";
    String rev="";
    for (int i = s.length()-1; i >=0; i--) {
        rev+=s.charAt(i);
    }
    System.out.println(rev);

I have set the index I to the last character of the given string and the condition is set to 0(ie the first character). 我已将索引I设置为给定字符串的最后一个字符,并将条件设置为0(即第一个字符)。 Hence the loop runs from the last character to the first character. 因此,循环从最后一个字符到第一个字符。 It extracts each of the characters to a given new String. 它将每个字符提取到给定的新String中。 Hope it helps! 希望能帮助到你!

You can just take another list go from last to first and display like this 您可以将另一个列表从倒数第一转到第一

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String s=br.readLine();

        ArrayList<Character> working = new ArrayList<Character>();
        ArrayList<Character> finished = new ArrayList<Character>();

        for (char ch: s.toCharArray()) {
            working.add(ch);
}
        for(int i = working.size() - 1 ; i>=0 ; i--){
            finished.add(working.get(i));
        }

        for(int j = 0 ; j < finished.size() ; j++){
        System.out.print(finished.get(j));
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM