0

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

4
  • 2
    Your problem is that you're splitting it into words rather than treating the whole thing as one word. Commented Jul 22, 2019 at 18:10
  • Consider how you can iterate through the string as if it were an array of characters, from the end to the beginning. As you do so, construct a new string from each character. Commented Jul 22, 2019 at 18:11
  • Are you allowed to call methods of class String? If yes, you may want to use toCharArray(), iterate over that and put every char to the beginning of another String. Commented Jul 22, 2019 at 18:20
  • String reversed = new StringBuilder("I am a coder").reverse(); Commented Jul 22, 2019 at 19:25

3 Answers 3

2

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);

}
Sign up to request clarification or add additional context in comments.

1 Comment

Spent some time on this, but it was worth it!
1
    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(i.e 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!

Comments

0

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));
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.