-1

I have to write a code with two classes, one reverses a string changing positions e.g

I love you becomes uoy evol I

and the other class reverses the string without changing positions e.g

I love you becomes I evol uoy.

I have a small code but I have failed to get a way if calling the methods in those classes.

What I have now is just the code that reverses the string in the first way. Any help is welcome.

class StringReverse2{
    public static void main(String[] args){

        String string="I love you";
        String reverse = new StringBuffer(string).  //The object created through StringBuffer is stored in the heap and therefore can be modified
        reverse().toString();                       //Here the string is reversed

        System.out.println("Old String:"+string);   //Prints out I love you
        System.out.println("New String : "+reverse);//Prints out the reverse  that is "uoy evol I"
    }
}
Maroun
  • 94,125
  • 30
  • 188
  • 241
Lizoan
  • 69
  • 2
  • 10
  • Just a suggestion, avoid using `Reserved Keywords` as variable/object names. It may cause an unexpected behavior sometimes. – Prerak Sola Feb 13 '15 at 14:21
  • 1
    @PrerakSola: such as ? – njzk2 Feb 13 '15 at 14:21
  • 3
    I would sure hope that it doesn't cause "unexpected" behavior "sometimes"; rather that it causes *expected* compiler error *always*. – Marko Topolnik Feb 13 '15 at 14:22
  • @njzk2 - such as `String string` – Ascalonian Feb 13 '15 at 14:29
  • @PrerakSola, @Acsalonian : `string` is not reserved keyword in Java, and anyway Java would not compile if a reserved keyword or an imported class name was used to name a variable. – njzk2 Feb 13 '15 at 14:36
  • 1
    @njzk2 - What @PrekSola was saying was to avoid `String string` and `ArrayList arrayList`. It makes it easier to read. And of course `string` all lower case isn't a reserved word, but it's just a reserved word in all lower case. Check out [this post about class vs clazz](http://stackoverflow.com/questions/2529974/why-do-java-programmers-like-to-name-a-variable-clazz). It is a good convention to follow – Ascalonian Feb 13 '15 at 14:40

2 Answers2

7

I won't show you a full solution, but will guide you, here's one way to do that:

  • split the String according to spaces (yourString.split("\\s+");)
  • iterate on the resulted array and reverse each String (you can use the same method you use for your first task)
  • construct a new String from the array

There are many more solutions, visit the String API and fuel your creative fire!

Maroun
  • 94,125
  • 30
  • 188
  • 241
  • Why did you do `split("\\s+")` instead of just `split(" ")`? – Ascalonian Feb 13 '15 at 14:30
  • @Ascalonian Because it's more general, `" "` is a regex that matches a single space, `\\s+` matches one **or more** spaces, so it'll work on string like `"abc____def_g_____hi jk"` (where `_` is a space) – Maroun Feb 13 '15 at 14:32
5

you can just use the reverse() method on a StringBuilder object

public class Testf {
   public static void main(String[] args){

        String string="I love you";
        String reverse = new StringBuilder(string).reverse().toString();    

        StringBuilder secondReverse = new StringBuilder();
        for (String eachWord : string.split("\\s+")){
            String reversedWord = new StringBuilder(eachWord).reverse().toString();
            secondReverse.append(reversedWord);
            secondReverse.append(" ");

        }

        System.out.println("Old String:"+string);   //Prints out I love you
        System.out.println("New String : "+reverse);//Prints out the reverse  that is "uoy evol I"
        System.out.println("Reversed word two: " + secondReverse.toString());
    }
}

API: http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html

ashosborne1
  • 2,884
  • 1
  • 13
  • 15
  • I didn't downvote, but read the question again, you didn't answer it. – Maroun Feb 13 '15 at 14:27
  • 1
    Basically, the code you have in your answer is the only part he has working in his question. You didn't read the question close enough. – Steve's a D Feb 13 '15 at 14:27
  • Thanks- changed to reflect question – ashosborne1 Feb 13 '15 at 14:32
  • 1
    Now it's correct, +1, but next time try not to provide full solution for a question that doesn't demonstrate minimal efforts. – Maroun Feb 13 '15 at 14:34
  • @MarounMaroun He has code that he wrote, I would call that displaying "minimal efforts". Although, the answer to this question could most likely be found across the web... – Evan Bechtol Feb 13 '15 at 14:36
  • @MarounMaroun thanks for your help, it has totally worked out but how do I put that in a separate class? I have to call that class in the main method in the parent class. – Lizoan Feb 13 '15 at 14:43
  • @MarounMaroun Meaning that this code generally goes to the other class, right? so that it is called in the parent class. – Lizoan Feb 13 '15 at 14:50
  • @Lizoan There's no hierarchy here, you can put that in separate class within the same package. – Maroun Feb 13 '15 at 14:58
  • @MarounMaroun please help i have just started java and am trying to get better, I might have not put it effort in this but these things don't end here, if I don't understand it now, how would I get it later, you just removed the best answer I had. Please get back. – Lizoan Feb 13 '15 at 14:59
  • @MarounMaroun youo talked something about Boolean results? From this example am going to create two classes outside the parent and evaluate these ideas. thanks – Lizoan Feb 13 '15 at 15:02
  • @Lizoan I really don't understand what you mean, please clarify. – Maroun Feb 13 '15 at 16:49