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"
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"
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;
}
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());
}