Click here to Skip to main content
14,769,805 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello I want to print the word java reversed so it prints avaj.
Posted
Updated 3-Jan-16 8:56am
v2
Comments
Thomas Daniels 3-Jan-16 13:29pm
   
Do you actually mean upside-down, or reversed? Your example just gives a reversed string, but upside-down it would be something like ɐʌɐɾ.
Member 12170330 3-Jan-16 13:43pm
   
Yes, I want just to reverse the sentence.
Thomas Daniels 3-Jan-16 13:57pm
   
Okay, I edited your question to reflect that.
PIEBALDconsult 3-Jan-16 14:02pm
   
We won't do your homework for you.

You can reverse a String in Java like this:
String s = "java";
String reversed = new StringBuilder(s).reverse().toString(); // "avaj"
   
Comments
   
5ed. :-)
—SA
Member 12170330 3-Jan-16 14:23pm
   
StringBuilder(s).reverse().toString(); whats mean this line????
Thomas Daniels 3-Jan-16 14:50pm
   
You create a StringBuilder instance initialized to the contents of s, you reverse it, and then you convert it to a string.
Member 12170330 3-Jan-16 20:22pm
   
ok,thanks
I would go a different way as i assume, OP is new to logic programming(from his/her question pattern).

Here we are traversing through the length of a word and backtracking them while printing i mean reversing. Have a look at it.

String str = "java";
String revStr = reverseWordByWord(str);

 public String reverseWordByWord(String str){
        int strLeng = str.length()-1;
        String reverse = "", temp = "";

        for(int i = 0; i <= strLeng; i++){
            temp += str.charAt(i);
            if((str.charAt(i) == ' ') || (i == strLeng)){
                for(int j = temp.length()-1; j >= 0; j--){
                    reverse += temp.charAt(j);
                    if((j == 0) && (i != strLeng))
                        reverse += " ";
                }
                temp = "";
            }
        }
        return reverse;
    }
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900