简体   繁体   中英

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

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:

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:

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). Hence the loop runs from the last character to the first character. It extracts each of the characters to a given new 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));
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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