How do i Increment Char Type for a String in Java? -
this works single character want string, have no idea how it.
class test { public static void main(string arg[]) { char c = 'a'; c = c + 1; system.out.println(c); } }
suppose string "hello world"
+1 increment return "ifmmp xpsme"
you have split string array of chars , add 1 each char:
public string sumtocharsatstring(string word) { stringbuffer b = new stringbuffer(); char[] chars = word.tochararray(); (char c : chars) { if(c != ' ') c = (char) (c + 1); b.append(c); } return b.tostring(); }
you dont need use stringbuffer, in order to save memory, practice.
Comments
Post a Comment