I am making a program that receives a string and returns the same string, interspersed with capital letters, that is, receives hello world (equal capital and lowercase letters) and writes HoLa MuNdO, regardless of the spaces. Now what he does to me is HoLa mUnDo, he takes into account the spaces. The code I have is, using ascii, without commands from those that do it directly.
Then another question I have is how to get to do these other two programs: HolA MundO that goes to uppercase or lowercase according to the entry the first and last letter and the other Hello World, only the first letters of the word, when entering by keyboard a chain.
import java.util.*;
public class amayus {
public static void main(String[] args) {
Scanner teclado = new Scanner(System.in);
System.out.println("Escriba una cadena");
String cad = teclado.nextLine();
int z = cad.length();
char c = 0;
for (int i = 0; i < z; i++) {
c = (char) cad.charAt(i);
if (i % 2 == 0) {
if (c >= 'A' && c <= 'Z' || c == ' ') { // MAYUSCULA->MANTIENE
System.out.print(c);
} else if (c >= 'a' && c <= 'z' || c == ' ') { // PASAR A
// MAYUSCULA
c = (char) (c - 'a' + 'A');
System.out.print(c);
}
} else if (i % 2 != 0) {
if (c >= 'a' && c <= 'z' || c == ' ') { //MINUSCULA -> MANTIENE
System.out.print(c);
} else if (c >= 'A' && c <= 'Z' || c == ' ') { // PASAR A
// MINUSCULA
c = (char) (c + 'a' - 'A');
System.out.print(c);
}
}
}
}
}