Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Create a function NextLetter which takes a string parameter and
- modifies it using the following algorithm:Replace every letter in the string with the letter next
- but one in the alphabet (c becomes e, z becomes b, A becomes C).
- Ignore numbers and symbols. Then capitalize every vowel in this new string (a, e, i, o, u)
- and finally return this modified string. The string will not be empty and not include spaces.
- Examples
- 1) Input- £7eBm Output- £7gDO
- 2) Input- Znb0y Output- Bpd0A
- */
- public class Sample {
- public static void main(String[] args) {
- String s = "Znb0y";
- System.out.println(nextLetter(s)); // Bpd0A
- System.out.println(nextLetter("abc")); //
- System.out.println(nextLetter("£7eBm")); // £7gDO
- }
- static boolean isVowel(char ch) {
- return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o'
- || ch == 'u';
- }
- static String nextLetter(String s) {
- char[] temp = s.toCharArray();
- for (int i = 0; i < temp.length; i++) {
- if (Character.isLetter(temp[i])) {
- if (temp[i] != 'z' && temp[i] != 'Z' && temp[i] != 'y' && temp[i] != 'Y') {
- temp[i] = (char) (temp[i] + 2);
- if (isVowel(temp[i])) {
- temp[i] = Character.toUpperCase(temp[i]);
- }
- } else { // if letter is z, Z, y, Y
- if (temp[i] == 'z') {
- temp[i] = 'b';
- }
- if (temp[i] == 'Z') {
- temp[i] = 'B';
- }
- if (temp[i] == 'y' || temp[i] == 'Y') {
- temp[i] = 'A';
- }
- }
- }
- }
- return String.valueOf(temp);
- }
- public static boolean starredLetters(String s) {
- // checks if String is empty
- if (s.isEmpty()) {
- return false;
- } else { // string is not empty
- // checks if String has at least one letter
- for (int j = 0; j < s.length(); j++) {
- if (Character.isLetter(s.charAt(j))) {
- break;
- }
- }
- int num = 0;
- for (int i = 0; i < s.length(); i++) {
- if (Character.isLetter(s.charAt(i))) {
- if ((i - 1) != -1 && (i + 1) < s.length()) {
- if (s.charAt(i - 1) != '*' || s.charAt(i + 1) != '*') {
- return false;
- }
- } else {
- return false;
- }
- } else {
- num++;
- if (num == s.length()) {
- return false;
- }
- }
- }
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment