bit of help on java...little quickie

Discussion in 'Programming and Homework Questions' started by alexpre888, Dec 11, 2013.

Thread Status:
Not open for further replies.
  1. alexpre888

    alexpre888 Notebook Consultant

    Reputations:
    7
    Messages:
    157
    Likes Received:
    25
    Trophy Points:
    41
    how do I write a program that rotates a given string n characters to the right. For example, if
    the input to my program is:

    rotatemeplease 4,

    then the output is:

    easerotatemepl



    anyone?


    EDIT:

    public class Rotatemetest
    {
    public static void main(String[] args)
    {
    String word = "rotatemeplease";
    String Four = word.substring(word.length() - 4, word.length());
    System.out.println(Four);
    }
    }

    the Output ended up like this:

    ease


    Grr...is there something I'm missing?
     
  2. Jarhead

    Jarhead Perfectly Sane

    Reputations:
    4,809
    Messages:
    11,810
    Likes Received:
    2,366
    Trophy Points:
    581
    I remember doing something similar in C that involved bitwise operations...

    Let me code up something real quick to see how to do it in Java...

    EDIT: Found my C code for bitshift operations. Are you familiar at all with bitwise operations?

    I tried converting my C code to Java, though there are some difference between the languages that I need to account for...

    In the meantime, you could check out StackOverflow for tips: http://stackoverflow.com/questions/10306647/how-to-bitwise-shift-a-binary-string-in-java
     
  3. ajnindlo

    ajnindlo Notebook Deity

    Reputations:
    265
    Messages:
    1,348
    Likes Received:
    87
    Trophy Points:
    66
    You are very close. If you look at your actual output and compare it to your desired output, what do you notice? Keep in mind you are simulating a rotate by using other functions. Can that be done in one step, i.e. one function?
     
  4. TheCodeBreaker

    TheCodeBreaker 7H3 1337

    Reputations:
    296
    Messages:
    988
    Likes Received:
    9
    Trophy Points:
    31
    LOL, nvm, just realized this was an old thread.


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

    String word = "rotatemeplease";
    String Four = word.substring(word.length() - 4, word.length());
    Four = Four + word.substring(0, word.length() - 4);
    System.out.println(Four);


    }
    }


    This should work. I do not have a compiler for java installed. Tell me if you would like it for the general case too.
     
Loading...
Thread Status:
Not open for further replies.

Share This Page