Promoting letters in a string to the next letter i

2020-02-26 12:12发布

I'm having issues figuring out how to get my code to increment the string that is given by user input so that when a user chooses to replace a letter like z it would go to a, b to c etc. The catch is I have to do this without using boolean. I am supposed to get this by using arithmetics to get the promotion from z to a from the users input. Plus must be only lower case letters from a-z. Any help would be appreciated thanks.

标签: java
7条回答
祖国的老花朵
2楼-- · 2020-02-26 12:56

Roll every character based on array length and when character value is greater then 'z' change value to 'a'

public static String stringRoll(String str, int[] arr) {
       char[] charArr = str.toCharArray();
       int valueOfZ = 'z';
      for(int i = 0; i < arr.length; i++){
          for(int j = 0; j < arr[i]; j++){
                int value = charArr[j] + 1;
               if(value > valueOfZ) {
                    charArr[j] = 'a';
                }else{
                  charArr[j] = (char) value;
           }
       }
   }
   return new String(charArr);
}
查看更多
登录 后发表回答