binibiningtinamoran

nextLetter()

Oct 14th, 2022 (edited)
1,329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.93 KB | None | 0 0
  1. /* Create a function NextLetter which takes a string parameter and
  2.    modifies it using the following algorithm:Replace every letter in the string with the letter next
  3.    but one in the alphabet (c becomes e, z becomes b, A becomes C).
  4.    Ignore numbers and symbols. Then capitalize every vowel in this new string (a, e, i, o, u)
  5.    and finally return this modified string. The string will not be empty and not include spaces.
  6.    Examples
  7.  
  8.    1) Input-  £7eBm      Output- £7gDO
  9.    2) Input-   Znb0y     Output- Bpd0A
  10.  */
  11.  
  12. public class Sample {
  13.  
  14.     public static void main(String[] args) {
  15.         String s = "Znb0y";
  16.         System.out.println(nextLetter(s)); // Bpd0A
  17.         System.out.println(nextLetter("abc")); //
  18.         System.out.println(nextLetter("£7eBm")); // £7gDO
  19.     }
  20.  
  21.     static boolean isVowel(char ch) {
  22.         return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o'
  23.                 || ch == 'u';
  24.     }
  25.  
  26.     static String nextLetter(String s) {
  27.         char[] temp = s.toCharArray();
  28.         for (int i = 0; i < temp.length; i++) {
  29.             if (Character.isLetter(temp[i])) {
  30.                 if (temp[i] != 'z' && temp[i] != 'Z' && temp[i] != 'y' && temp[i] != 'Y') {
  31.                     temp[i] = (char) (temp[i] + 2);
  32.                     if (isVowel(temp[i])) {
  33.                         temp[i] = Character.toUpperCase(temp[i]);
  34.                     }
  35.                 } else { // if letter is z, Z, y, Y
  36.                     if (temp[i] == 'z') {
  37.                         temp[i] = 'b';
  38.                     }
  39.                     if (temp[i] == 'Z') {
  40.                         temp[i] = 'B';
  41.                     }
  42.                     if (temp[i] == 'y' || temp[i] == 'Y') {
  43.                         temp[i] = 'A';
  44.                     }
  45.                 }
  46.             }
  47.         }
  48.         return String.valueOf(temp);
  49.     }
  50.  
  51.  
  52.     public static boolean starredLetters(String s) {
  53.         // checks if String is empty
  54.         if (s.isEmpty()) {
  55.             return false;
  56.         } else { // string is not empty
  57.             // checks if String has at least one letter
  58.             for (int j = 0; j < s.length(); j++) {
  59.                 if (Character.isLetter(s.charAt(j))) {
  60.                     break;
  61.                 }
  62.             }
  63.             int num = 0;
  64.             for (int i = 0; i < s.length(); i++) {
  65.                 if (Character.isLetter(s.charAt(i))) {
  66.                     if ((i - 1) != -1 && (i + 1) < s.length()) {
  67.                         if (s.charAt(i - 1) != '*' || s.charAt(i + 1) != '*') {
  68.                             return false;
  69.                         }
  70.                     } else {
  71.                         return false;
  72.                     }
  73.                 } else {
  74.                     num++;
  75.                     if (num == s.length()) {
  76.                         return false;
  77.                     }
  78.  
  79.                 }
  80.             }
  81.         }
  82.         return true;
  83.     }
  84. }
  85.  
Advertisement
Add Comment
Please, Sign In to add comment