Reemplace cada secuencia de consonantes con su longitud en la string dada

Dada una string str que consta de caracteres en minúsculas. La tarea es reemplazar la secuencia consecutiva de consonantes con su longitud.
Ejemplos: 
 

Entrada: str = “abcdeiop” 
Salida: a3eio1 La 
string dada contiene 2 secuencias de consonantes “bcd” y “p” con longitudes de 3 y 1.
Entrada: str = “abecidofu” 
Salida: a1e1i1o1u 
 

Enfoque: haga una nueva string que esté libre de consonantes. Para ello, tenemos que iterar la string dada. Si encontramos una vocal, se agregará a la nueva string tal como está. Si encontramos una consonante, entonces tenemos que encontrar la longitud de la secuencia de consonantes completa y luego agregar su longitud a la nueva string. 
A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the converted string
// after replacing every consonant sequence
// with its length
string replaceConsonants(string str)
{
 
    // To store the resultant string
    string res = "";
    int i = 0, count = 0;
 
    // Checking each character
    // for consonant sequence
    while (i < str.length()) {
 
        // Count the length of consonants sequence
        if (str[i] != 'a'
            && str[i] != 'e'
            && str[i] != 'i'
            && str[i] != 'o'
            && str[i] != 'u') {
            i++;
            count++;
        }
        else {
 
            // Add the length in the string
            if (count > 0)
                res += to_string(count);
 
            // Add the vowel
            res += str[i];
 
            i++;
            count = 0;
        }
    }
 
    // Check for the last consonant sequence
    // in the string
    if (count > 0)
        res += to_string(count);
 
    // Return the resultant string
    return res;
}
 
// Driver code
int main()
{
    string str = "abcdeiop";
    cout << replaceConsonants(str);
 
    return 0;
}

Java

// Java implementation of the approach
import java.util.*;
import java.lang.*;
 
class GFG {
 
    // Function to return the converted string
    // after replacing every consonant sequence
    // with its length
    static String replaceConsonants(String str)
    {
 
        // To store the resultant string
        String res = "";
        int i = 0, count = 0;
 
        // Checking each character
        // for consonant sequence
        while (i < str.length()) {
 
            // Count the length of consonants sequence
            if (str.charAt(i) != 'a'
                && str.charAt(i) != 'e'
                && str.charAt(i) != 'i'
                && str.charAt(i) != 'o'
                && str.charAt(i) != 'u') {
                i++;
                count++;
            }
            else {
 
                // Add the length in the string
                if (count > 0)
                    res += count;
 
                // Add the vowel
                res += str.charAt(i);
 
                i++;
                count = 0;
            }
        }
 
        // Check for the last consonant sequence
        // in the string
        if (count > 0)
            res += count;
 
        // Return the resultant string
        return res;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        String str = "abcdeiop";
        System.out.println(replaceConsonants(str));
    }
}
 
// This code is contributed by Code_Mech.

Python3

# Python3 implementation of the above approach
 
# Function to return the converted string
# after replacing every consonant sequence
# with its length
def replaceConsonants(string) :
 
    # To store the resultant string
    res = "";
    i = 0; count = 0;
 
    # Checking each character
    # for consonant sequence
    while (i < len(string)) :
 
        # Count the length of consonants sequence
        if (string[i] != 'a'
            and string[i] != 'e'
            and string[i] != 'i'
            and string[i] != 'o'
            and string[i] != 'u') :
            i += 1;
            count += 1;
         
        else :
 
            # Add the length in the string
            if (count > 0) :
                res += str(count);
 
            # Add the vowel
            res += string[i];
 
            i += 1
            count = 0;
 
    # Check for the last consonant
    # sequence in the string
    if (count > 0) :
        res += str(count);
 
    # Return the resultant string
    return res;
 
# Driver code
if __name__ == "__main__" :
     
    string = "abcdeiop";
    print(replaceConsonants(string));
 
# This code is contributed by Ryuga

C#

using System;
 
// c# implementation of the approach
 
public class GFG {
 
    // Function to return the converted string
    // after replacing every consonant sequence
    // with its length
    public static string replaceConsonants(string str)
    {
 
        // To store the resultant string
        string res = "";
        int i = 0, count = 0;
 
        // Checking each character
        // for consonant sequence
        while (i < str.Length) {
 
            // Count the length of consonants sequence
            if (str[i] != 'a'
                && str[i] != 'e'
                && str[i] != 'i'
                && str[i] != 'o'
                && str[i] != 'u') {
                i++;
                count++;
            }
            else {
 
                // Add the length in the string
                if (count > 0) {
                    res += count;
                }
 
                // Add the vowel
                res += str[i];
 
                i++;
                count = 0;
            }
        }
 
        // Check for the last consonant sequence
        // in the string
        if (count > 0) {
            res += count;
        }
 
        // Return the resultant string
        return res;
    }
 
    // Driver code
    public static void Main(string[] args)
    {
        string str = "abcdeiop";
        Console.WriteLine(replaceConsonants(str));
    }
}
 
// This code is contributed by Shrikant13

Javascript

<script>
      // JavaScript implementation of the approach
 
      // Function to return the converted string
      // after replacing every consonant sequence
      // with its length
      function replaceConsonants(str) {
        // To store the resultant string
        var res = "";
        var i = 0,
          count = 0;
 
        // Checking each character
        // for consonant sequence
        while (i < str.length) {
          // Count the length of consonants sequence
          if (
            str[i] !== "a" &&
            str[i] !== "e" &&
            str[i] !== "i" &&
            str[i] !== "o" &&
            str[i] !== "u"
          ) {
            i++;
            count++;
          } else {
            // Add the length in the string
            if (count > 0) res += count.toString();
 
            // Add the vowel
            res += str[i];
 
            i++;
            count = 0;
          }
        }
 
        // Check for the last consonant sequence
        // in the string
        if (count > 0) res += count.toString();
 
        // Return the resultant string
        return res;
      }
 
      // Driver code
      var str = "abcdeiop";
      document.write(replaceConsonants(str));
       
      // This code is contributed by rdtank.
    </script>
Producción: 

a3eio1

 

Publicación traducida automáticamente

Artículo escrito por SURENDRA_GANGWAR y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *