how to show a word but each letter increased by 3?

1

I need to make a program that when entering a word shows me that word but each letter increased by 3 if I enter "hola" that shows me "krñd"

    
asked by Paul Julio 17.05.2017 в 07:04
source

2 answers

1

You would take the String with the word and, since a String is a matrix of chars , you separate them one by one, convert them to int , add them 3 and convert them back to String :

String palabra="hola";
String nuevaPalabra="";
for(int i=0;i<palabra.length;i++){
   char letra= palabra.charAt[i];
   int j= (int)letra;
   int nuevaJ=j+3;
   char nuevaLetra= (char)nuevaJ;  
   nuevaPalabra+=nuevaLetra;
}
    
answered by 17.05.2017 в 08:44
0

You simply go through the entered word with a for iterating for each character and with a matrix with the alphabet you are looking for the positions.

Here is a complete example.

try {
                    String[] abc=new String[]{"a","b","c","d","e","f","g","h","i","j","k","l","m","n","ñ","o","p","q","r","s","t","u","v","w","x","y","z"};
                    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                    String texto="";
                    String resultado="";

                    do{
                        System.out.println("Introduce la palabra:");
                        texto=br.readLine();
                        texto=texto.toLowerCase();
                        resultado="";
                        for(int i=0;i<texto.length();i++){
                            resultado+=abc[Arrays.asList(abc).indexOf(String.valueOf(texto.charAt(i)))+3];
                        }
                        System.out.println("Resultado: "+resultado);
                    }while(!texto.equals("0"));


                } catch (IOException e) {
                    System.out.println("Exception: "+e.getClass()+" --> "+e.getMessage());
                }
    
answered by 17.05.2017 в 08:49