khanam
khanam

Reputation: 345

String word reverse in Java giving wrong result?

Here is my code to print string characters reversed in Java without using any API. But it's not working properly. Can anybody help me to correct it?

public static void main(String args[]) {
    String input = "I am test";
    String result = "";
    for (int i = input.length() - 1; i > 0; i--) {
        Character c = input.charAt(i);
        if (c != ' ') {
            result = c + result;
        } else {
            System.out.println(result + " ");
        }
    }
}

It is giving output "test amtest", while the output should be "test am I".

Please help me to get exact output without using predefined methods or API's.

Upvotes: 7

Views: 808

Answers (5)

Deepak Bala
Deepak Bala

Reputation: 1

public static void main(String args[]){
    String input = "I am test";
    String result="";
    for(int i=input.length()-1;i>=0;i--){
        result=result+input.charAt(i);
    }
    System.out.println(result);
}

Upvotes: -1

Neelam Kapoor
Neelam Kapoor

Reputation: 79

You can try recursion as well -

public static void main(String args[]) {
    String input = "I am test";
    List<String> listOfString = Arrays.asList(input.split(" "));

    System.out.println(reverseString(listOfString));

}

private static String reverseString(List<String> input) {
    int n = input.size();
    String result = "";
    if(input.isEmpty()){
        return result;
    }

    if(n>1){
   /*adding last element with space and changes the size of list as well
        test + " " + [am, I] 
        test + " " + am + " " + [I]*/

        result = input.get(n-1) + " " + reverseString(input.subList(0, n-1));
    }else{
        result = input.get(n-1);
    }               
    return result;

}

hope it helps.

Upvotes: 1

Grady G Cooper
Grady G Cooper

Reputation: 1064

public static void main(String args[]) {
    String input = "I am test";
    String result = "";

    String[] frags = input.split(" ");
    for (int i = frags.length - 1; i >= 0; i--) {
        System.out.print(frags[i] + " ");
        }
    System.out.println();
 }

Upvotes: 4

user4624062
user4624062

Reputation:

try

public static void main(String args[]) {
    String input = "I am test";
    String result = "";
    int start=input.length()-1;
    for (int i = input.length()-1; i >=0; i--) {
        Character c = input.charAt(i);
        if (c == ' ') {
            for(int j=i+1;j<=start;j++)
                result +=input.charAt(j);
            result+=" ";
            start=i-1;
        }
        else if (i==0)
        {
            for(int j=0;j<=start;j++)
                result +=input.charAt(j);
        }
    }
    System.out.println(result);
}//It is giving output as test amtest
//output should be : test am I

Upvotes: 4

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726569

There are four problems with your implementation:

  • You do not go all the way down to zero,
  • You put an end of line after each printout in the loop,
  • You do not print the "tail" result after the loop is over, and
  • You do not clear out result after printing it in the loop.

Fixing these issues will give you proper output (demo).

Upvotes: 14

Related Questions