Pass a java string to uppercase and lowercase

3

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);

            }
        }
    }

  }

}
    
asked by Fernando 28.01.2017 в 13:21
source

1 answer

5

First Part

To the first part of your question, about how to toggle between capital letters, I could show you this:

public static void main(String[] args) {

        Scanner teclado = new Scanner(System.in);

        System.out.println("Escriba una cadena:");
        String cadena = teclado.nextLine();

        char caracter;
        int contadorCambio = 0;

        for (int i = 0; i < cadena.length(); i++) {

            caracter = (char) cadena.charAt(i);

            if (caracter == ' ') {

                //Imprimimos el espacio en blanco
                System.out.print(caracter);

            } else {

                if (contadorCambio % 2 == 0) {

                    if (Character.isUpperCase(caracter)) {

                        // MAYUSCULA->MANTIENE
                        System.out.print(caracter);

                    } else if (Character.isLowerCase(caracter)) {

                        // PASAR A MAYUSCULA
                        caracter = (char) (caracter - 'a' + 'A');
                        System.out.print(caracter);

                    }

                } else {

                    if (Character.isLowerCase(caracter)) {

                        //MINUSCULA -> MANTIENE
                        System.out.print(caracter);

                    } else if (Character.isUpperCase(caracter)) {

                        // PASAR A MINUSCULA
                        caracter = (char) (caracter + 'a' - 'A');
                        System.out.print(caracter);

                    }

                }

                contadorCambio++;
            }

        }

    }

Result:

  

Enter a string:

     

hello world

     

HoLa MuNdO

I have organized the code a bit, and also do not use the ASCII (Old Customs), in that case you can replace the conditionals with ASCII just as you had it , although in my case, I prefer to use isUpperCase () and isLowerCase () since they are functions made on purpose to save the programmer that type of conditions by playing with the code > ASCII .

Obviously, if you want to use the ASCII code, just change the conditionals, like this:

Character.isLowerCase (character) POR character > = 'a' & & character < = 'a' Character.isUpperCase (character) POR character > = 'A' & & character < = 'Z'

Second Part

Regarding the problem you indicate (Leave only the initials as capital letters), this would be my proposed solution:

public static void main(String[] args) {

    Scanner teclado = new Scanner(System.in);

    System.out.println("Escriba una cadena:");
    String cadena = teclado.nextLine();

    char caracter;
    boolean bandera = true;    // TRUE si se empieza una nueva palabra

    for (int i = 0; i < cadena.length(); i++) {

        caracter = (char) cadena.charAt(i);

        if (caracter == ' ') {

            //Imprimimos el espacio en blanco
            System.out.print(caracter);
            bandera = true;

        } else {

            if (bandera == true) {

                if (Character.isLowerCase(caracter)) {

                    // Pasar a Mayuscula
                    caracter = (char) (caracter - 'a' + 'A');
                    System.out.print(caracter);

                } else {

                    System.out.print(caracter);

                }

                bandera = false;
            } else {

                if (Character.isUpperCase(caracter)) {

                    // Pasar a Minuscula
                    caracter = (char) (caracter + 'a' - 'A');
                    System.out.print(caracter);

                } else {

                    System.out.print(caracter);

                }

            }

        }

    }

}

Result:

  

Enter a string:

     

hOlA MUNDO this is a PruEba

     

Hello World This Is A Test

    
answered by 28.01.2017 / 13:59
source