How to identify a word within a String and replace it completely with another symbol (#)?

1

The program should read a given String and find certain words with the following characteristics: start with 'p' and end with 'r' and start with 'h' and anywhere in the word have the letter 'o' . Since the word is identified it must be completely replaced with the symbol '#', keeping the total number of characters of the original word , (ie, the same amount of '#' must be the same than the number of letters the word originally has.

The program must return the complete String with the words identified replaced with the '#'. For example:

  

String text="the ants are eating what I just planted";

     

output: the ######## are eating what I just #######

I leave attached the code that I have so far:

public static void main(String[] args) {

    Scanner leer = new Scanner(System.in);
    System.out.println("TEXTO:");

    String texto = leer.nextLine();
    String arreglop[] = new String[texto.split(" ").length];


    for (int i = 0; i < texto.split(" ").length; i++) {
        char arreglol[] = new char[arreglop.length];
        arreglop[i] = texto.split(" ")[i];

        for (int j = 0; j < arreglop[i].length(); j++) {
            arreglol[j] = arreglop[i].charAt(j);

            if (arreglol[j] == 'p' || arreglol[j] == 'P') {

                if (arreglol[arreglop[i].length()] == 'r' || arreglol[arreglop[i].length()] == 'R') {

                    for (int k = 0; k < arreglop[i].length(); k++) {
                        arreglol[k] = '#';
                        System.out.print(arreglol[k]);
                    }
                }
            } else if (arreglol[j] == 'h' || arreglol[j] == 'H') {

                for (int k = 0; k < arreglop[i].length(); k++) {

                    if (arreglol[k] == 'o' || arreglol[k] == 'O') {

                        for (int l = 0; l < arreglop[i].length(); l++) {
                            arreglol[l] = '#';
                            System.out.print(arreglol[l]);
                        }
                    }
                }
            } else {
                System.out.print(arreglop[i]);
            }
        }
    }        
}
    
asked by Jonathan Lin C. 07.10.2017 в 05:51
source

1 answer

0

I have a similar exercise that I think will be helpful.

The code would be the following:

    try
    {
        String palabra = JOptionPane.showInputDialog(this, "Escriba una palabra");
        String[] palabras = palabra.split(" ");
        for (int i = 0; i < palabras.length; i++)
        {
            if(String.valueOf(palabras[i].charAt(0)).equals("P") || String.valueOf(palabras[i].charAt(0)).equals("p"))
            {
                if(String.valueOf(palabras[i].charAt(palabras[i].length()-1)).equals("R") || String.valueOf(palabras[i].charAt(palabras[i].length()-1)).equals("r"))
                {
                    JOptionPane.showMessageDialog(this, "Palabra encontrada que empieza con P y termina con R:\n"+palabras[i]);
                    int longitud = palabras[i].length();
                    palabras[i] = "";
                    for (int j = 0; j < longitud; j++)
                    {
                        palabras[i] = palabras[i] + "#";
                    }
                }
            }
            if(String.valueOf(palabras[i].charAt(0)).equals("H") || String.valueOf(palabras[i].charAt(0)).equals("h"))
            {
                for (int j = 0; j < palabras[i].length(); j++)
                {
                    if(String.valueOf(palabras[i].charAt(j)).equals("O") || String.valueOf(palabras[i].charAt(j)).equals("o"))
                    {
                        JOptionPane.showMessageDialog(this, "Palabra encontrada que empieza con H y contiene la letra O:\n"+palabras[i]);
                        int longitud2 = palabras[i].length();
                        palabras[i] = "";
                        for (int k = 0; k < longitud2; k++)
                        {
                            palabras[i] = palabras[i] + "#";
                        }
                    }
                }
            }
        }
        String resultado = "";
        for (int i = 0; i < palabras.length; i++)
        {
            resultado = resultado + palabras[i] + " ";
        }
        JOptionPane.showMessageDialog(this, "Resultado:\n"+resultado);
    }
    catch(Exception e)
    {
        System.out.println("formularios.jFrmePruebas1.jButton4ActionPerformed Error:\n"+e);
    }

If you have any questions, write me in the comments

    
answered by 07.10.2017 / 06:41
source