简体   繁体   中英

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.

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

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

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.

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

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