Results 1 to 4 of 4
  1. #1
    DreamNaut is offline Member
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default Shifting String Letters

    Hello, I was wondering if somone could steer me in the right direction with shifting each letter in a string over n times (in the alphabet) without using an array.

    I was looking at the substring method, but im not quite sure on how to utilize it to perform this operation.

    Any help is appreciated thanks
    Last edited by DreamNaut; 10-25-2010 at 10:27 PM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    The String method charAt() will return the char value corresponding to a given position in a string. So if the string is "random" then charAt(2) will return 'n'.

    char values are actually numeric values: that is you can add/subtract etc with them. So if you wanted you could "shift" the 'n' by two places with the expression 'n'+2. This expression is actually the same as the char value 'p'. The chances are that your "shifting" involves wrapping around at some point so that 'z' --> 'b' or whatever. The arithmetic you use on the char values will have to implement that.

    (Read your textbook or online about char)

    You could use substring to "extract" a piece of a string. But it seems to me that it is more straightforward to use a numeric data type like char.

    Finally note that strings are immutable (=="don't change their content"). This is reflected in the fact that we have a charAt() but no corresponding setCharAt(). Since you can't change a string the next best thing is to create a new string based on the arithmetically altered char values you have constructed. You can concatenate (==join together) things to make string. So, eg, ""+'t'+'c'+'p' will be a string equal to "tcp".

    Putting these ideas together you have something that might be expressed somewhat abstractly as:

    Java Code:
    "random"   SHIFT BY 2
    
    charAt(0)  'r'  'r'+2  ('t') ""+('r'+2)                  "t"
    charAt(1)  'a'  'a'+2  ('c')           +('a'+2)          "tc"
    charAt(2)  'n'  'n'+2  ('p')                   +('n'+2)  "tcp"
    ...

    Clearly some sort of loop is involved here.

    If you decide to follow this route and you get stuck, post code so we can see what you're doing.
    Last edited by pbrockway2; 10-26-2010 at 09:12 PM.

  3. #3
    DreamNaut is offline Member
    Join Date
    Oct 2010
    Posts
    12
    Rep Power
    0

    Default

    Thanks, that helps alot

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

Similar Threads

  1. counting letters in a string
    By beandip408 in forum New To Java
    Replies: 12
    Last Post: 09-29-2010, 02:44 PM
  2. generate random letters inbetween a string
    By greg677 in forum New To Java
    Replies: 1
    Last Post: 05-04-2010, 06:06 AM
  3. increment letters from a string
    By iluvjava in forum New To Java
    Replies: 24
    Last Post: 03-22-2010, 06:18 PM
  4. [SOLVED] Shifting an array
    By VeasMKII in forum New To Java
    Replies: 2
    Last Post: 02-04-2009, 07:18 PM
  5. validating a string for numbers and letters?
    By lockmac in forum New To Java
    Replies: 1
    Last Post: 08-09-2007, 10:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •