Home > Enterprise >  What is the best way to replace a letter with the letter following it in the alphabet in Java?
What is the best way to replace a letter with the letter following it in the alphabet in Java?

Time:08-25

I'm a programming newbie and I am doing a coderbyte exercise that says " Replace every letter in the string with the letter following it in the alphabet (ie. c becomes d, z becomes a)"

i'm thinking of the following methods:

  1. declare a string called "abcdefghijklmnopqrstuvxyz" and compare each string's char index position with the alphabet's index position, and then just bring the alphabet char that is located at the i 1 index location. But I don't know how it would work from z to a.
  2. I've seen some techniques using ASCII values for every char but I've never done that before and not sure how it works
  3. convert the given string into a char[] array, but then I'm not sure how I would tell the system to get me the next alphabet char

What would be the easiest way to do this?

EDIT this is my code so far, but it doesn't work.

import java.util.*; 
import java.io.*;

class Main {

  public static String LetterChanges(String str) {
    // code goes here 
    String alphabet = "abcdefghijklmnopqrstuvwxyz";
    String newWord = "";

    for (int i = 0; i < str.length(); i  ){
      for (int j = 0; j < alphabet.length(); i  ){
        if (str[i] == alphabet[i]){
          if (alphabet[i 1].isVowel()){
          newWord = newWord   toUpperCase(alphabet[i 1]);
          }
          else{
            newWord = newWord   alphabet[i 1];
          }
        }
      }
    }
    return str;
  }

  public static void main (String[] args) {  
    // keep this function call here     
    Scanner s = new Scanner(System.in);
    System.out.print(LetterChanges(s.nextLine())); 
  }

}

Can't I ask for the index position of a Char that is a part of a String? in C I could do that. Other than that not sure why it doesn't work.

CodePudding user response:

I would definitely go with method 1.

I believe what you're looking for is the indexOf method on a String. First of, I would create a method that given a character finds the next letter in the alphabet and return that. This could be done by finding the letter in your alphabet string and then fetch the letter at index 1. As you also pointed out you would need to take care of the edge case to turn 'z' into 'a', could by done with an if-statement or by having an extra letter 'a' at the end of your alphabet string.

Now all that remains to do is create a loop that runs over all characters in the message and calls the previously made method on that character and constuct a new string with the output.

Hope this helps you figure out a solution.

CodePudding user response:

Assuming that there would be only lower case English letters in the given String the most performant way would be to add 1 to every character, and use either if-statement checking whethe the initial character was z or use the modulo operator % as @sp00m has pointed out in the comment.

Performing a search in the alphabetic string (option 1 in your list) is redundant, as well extracting array char[] from the given string (option 3).

Checking the edge case:

public static String shiftLetters(String str) {
    
    StringBuilder result = new StringBuilder();
    
    for (int i = 0; i < str.length(); i  ) {
        char next = str.charAt(i);
        if (next == 'z') result.append('a');   // checking the edge case
        else result.append((char) (next   1));
    }
    
    return result.toString();
}

Applying modulo operator:

public static String shiftLetters(String str) {
    
    StringBuilder result = new StringBuilder();
    
    for (int i = 0; i < str.length(); i  ) {
        char next = (char) ((str.charAt(i) - 'a'   1) % 26   'a');
        result.append(next);
    }
    
    return result.toString();
}

main()

public static void main(String[] args) {
    System.out.println(shiftLetters("abc"));
    System.out.println(shiftLetters("wxyz"));
}

Output:

bcd   // "abc"
xyza  // "wxyz"
  • Related