Increment String chracter

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • questionit
    Contributor
    • Feb 2007
    • 553

    Increment String chracter

    Hi Experts

    how to implement this in Java . example problem:

    1) Input string e.g "HAL"
    2) Output IBM ( each letter in string replaced with next character whatever comes in ABC...

    Thanks
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    Originally posted by questionit
    Hi Experts

    how to implement this in Java . example problem:

    1) Input string e.g "HAL"
    2) Output IBM ( each letter in string replaced with next character whatever comes in ABC...

    Thanks
    you could use a StringBuffer
    http://java.sun.com/j2se/1.3/docs/api/java/lang/StringBuffer.ht ml

    which is similar to a String but you can change the contents, e.g.
    Code:
      StringBuffer s=new StringBuffer("HAL");
      s.setCharAt(0,(char)(s.charAt(0)+1));
    incremenets the first character of the string H to I so it becomes
    IAL
    you could use a loop to increment all the characters

    Comment

    • azar04
      New Member
      • May 2017
      • 1

      #3
      if i am pass "ZAL" Output is "[AL"
      It is wrong.

      Comment

      • chaarmann
        Recognized Expert Contributor
        • Nov 2007
        • 785

        #4
        Please try to understand the example given. It is not wrong, but it shows you how to replace the first character only. Just do the same for the second and third character. He asks you to do it with a loop yourself.

        Another problem is: what should come after "Z"? You say "next character" which is "[" and correct. But if you mean "next letter" then you have define what should happen with the last letter:
        1.) Should "Z" stay "Z" because here is no next letter?
        2.) Should you start from the beginning of the uppercase letters? that means "A"
        3.) Should you start from the beginning of the lowercase letters? that means "a"? (second series of letters)
        4.) Should you start with language specific letters? In Germany in many sorting application you would start with all the addition letters (German umlauts like "Ä", "Ö", "ß").
        5.) Or should the german umlauts follow directly inside the alphabet, like in the telephone book sorting? (like "Ä" follows of "A" or "Ö" follows of "O". Just think of all the collation orders (of letters) where you have to pick one, for example in the mySql database.
        6.) If you really mean "character" instead of "letter" as you have written it, then you must define the character set. In other charater sets there are characters follwing "Z" which are different from ASCII character "[". Just think of the EBCDIC character set...

        Comment

        Working...