Show the letters of a string and how many there are

2

I have a code that shows me the letters of a name and the number of letters in that name. But when he prints the letters and how many there are, he repeats the letters he has already read and tells me the spaces. Here the code:

create or REPLACE PROCEDURE nombre is
begin
    declare 
        nombre VARCHAR(100) := 'Pepito perez';
        letra VARCHAR(1);
        letra2 VARCHAR(1);
        contador int;                       
    begin 
        for i in 1..LENGTH(nombre) loop
            contador :=0;
            letra := SUBSTR(nombre, i, 1);
            for j in 1..LENGTH(nombre) loop
                letra2 := SUBSTR(nombre, j, 1);                 
                    if (letra2 = letra) then
                        contador := contador +1;
                    end if;

            end loop;
            begin
                dbms_output.put_line(letra||': '|| contador);
            end;
        end loop;   
    end;
end;

The result it throws is:

SQL> exec nombre
p: 3
e: 3
p: 3
i: 1
t: 1
o: 1
: 1
p: 3
e: 3
r: 1
e: 3
z: 1

What I need is that it only shows for example the P and how many there are, but not that they are repeated.

    
asked by Jeison Velásquez 26.02.2017 в 03:15
source

1 answer

2

To avoid repeating the letter and the quantity found in the chain, we need to know if the character in question was already printed. For this we are going to do a function that finds out that.

We'll call the function letter_and_shown . The function will receive three arguments:

  • letter: the character we want to find out if it has already been printed.
  • name: the full text string you are analyzing.
  • current iter: the current location where the letter is positioned in the main loop.
  • The task of the function is to reverse the name from the location that you pass by parameter and find out if the letter you also receive is in a previous location.

    Example:

    letra = 'p'
    nombre = 'Pepito perez'
    ubicacion = 3
    

    The function analyzes the 'Pe' portion of name in the following way:

    'e' = 'p'
    'P' = 'p'
    

    and if it finds a match, it assumes that the letter has already been shown on the screen.

    The function is as follows:

    function letra_ya_mostrada(
        i_letra     in varchar2, 
        i_nombre    in varchar2, 
        i_iter_actual in number
    )  return varchar2 is
    
        mostrado varchar2(1) := 'N';
    
    begin
    
        for i in reverse 1..i_iter_actual - 1 loop
            if ( UPPER(i_letra) = UPPER(SUBSTR(i_nombre, i, 1)) ) then
                mostrado := 'S';
            end if;
        end loop;
    
        return mostrado;
    
    end;
    

    We are going to modify the next block to evaluate if the letter was already shown.

    begin
        if letra_ya_mostrada(letra, nombre, i) = 'N' then
            dbms_output.put_line(letra||': '|| contador);
        end if;
    end;
    

    The procedure would be as follows.

    create or REPLACE PROCEDURE nombre is
    
        function letra_ya_mostrada(
            i_letra     in varchar2, 
            i_nombre    in varchar2, 
            i_iter_actual in number
        )  return varchar2 is
    
            mostrado varchar2(1) := 'N';
    
        begin
    
            for i in reverse 1..i_iter_actual - 1 loop
                if ( UPPER(i_letra) = UPPER(SUBSTR(i_nombre, i, 1)) ) then
                    mostrado := 'S';
                end if;
            end loop;
    
            return mostrado;
        end;
    
    begin
    
        declare 
            nombre VARCHAR(100) := 'Pepito perez';
            letra VARCHAR(1);
            letra2 VARCHAR(1);
            contador int;                       
        begin 
            for i in 1..LENGTH(nombre) loop
                contador :=0;
                letra := SUBSTR(nombre, i, 1);
                for j in 1..LENGTH(nombre) loop
                    letra2 := SUBSTR(nombre, j, 1);                 
                        if (UPPER(letra2) = UPPER(letra)) then
                            contador := contador +1;
                        end if;
    
                end loop;
    
                begin
                    if letra_ya_mostrada(letra, nombre, i) = 'N' then
                        dbms_output.put_line(letra||': '|| contador);
                    end if;
                end;
    
            end loop;   
        end;
    
    end;
    

    Greetings !!

        
    answered by 06.04.2017 в 21:22