Reemplace las consonantes con las siguientes consonantes inmediatas alfabéticamente en una string

Dada una string que contiene alfabetos ingleses en minúsculas. La tarea es reemplazar cada consonante con la siguiente consonante inmediata que viene en los alfabetos ingleses.
Digamos que tenemos que reemplazar character  a    , será reemplazado por  b    . Otro ejemplo, digamos que tenemos que reemplazar el carácter  d    , la siguiente consonante inmediata es  F    , por lo tanto  d    , será reemplazada por  F    .
Nota : si el carácter es ‘z’, busque circularmente en los alfabetos ingleses la siguiente consonante, es decir, reemplácela con ‘b’.
Ejemplos
 

Input : str = "geeksforgeeks"
Output : heeltgosheelt

Input : str = "gfg"
Output : hgh

Acercarse: 
 

  • Iterar los elementos de string de izquierda a derecha.
  • Si el elemento de la string es consonante, compruebe el siguiente alfabeto inmediato de este elemento.
  • Si el siguiente alfabeto inmediato es una consonante, reemplácelo con este alfabeto. Si es una vocal, reemplace el elemento de string con el segundo alfabeto inmediato ya que no hay vocales consecutivas en los alfabetos ingleses.

A continuación se muestra la implementación del programa anterior: 
 

C++

// C++ program of above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if a character is
// vowel or not
bool isVowel(char ch)
{
    if (ch != 'a' && ch != 'e' && ch != 'i' && ch != 'o'
        && ch != 'u')
        return false;
 
    return true;
}
 
// Function that replaces consonant with
// next immediate consonant alphabetically
string replaceConsonants(string s)
{
    // Start traversing the string
    for (int i = 0; i < s.length(); i++) {
 
        if (!isVowel(s[i])) {
 
            // if character is z,
            // than replace it with character b
            if (s[i] == 'z')
                s[i] = 'b';
          // if character is Z,
            // than replace it with character B
            else if (s[i] == 'Z')
                {
                    s[i] = 'B';
                }
            // if the alphabet is not z
            else {
 
                // replace the element with
                // next immediate alphabet
                s[i] = (char)(s[i] + 1);
 
                // if next immediate alphabet is vowel,
                // than take next 2nd immediate alphabet
                // (since no two vowels occurs consecutively
                // in alphabets) hence no further
                // checking is required
                if (isVowel(s[i]))
                    s[i] = (char)(s[i] + 1);
            }
        }
    }
 
    return s;
}
 
// Driver code
int main()
{
    string s = "geeksforgeeks";
 
    cout << replaceConsonants(s);
 
    return 0;
}

Java

// Java program of above approach
class GFG {
 
    // Function to check if a character is
    // vowel or not
    static boolean isVowel(char ch)
    {
        if (ch != 'a' && ch != 'e' && ch != 'i' && ch != 'o'
            && ch != 'u') {
            return false;
        }
        return true;
    }
 
    // Function that replaces consonant with
    // next immediate consonant alphabetically
    static String replaceConsonants(char[] s)
    {
        // Start traversing the string
        for (int i = 0; i < s.length; i++) {
            if (!isVowel(s[i])) {
 
                // if character is z,
                // than replace it with character b
                if (s[i] == 'z') {
                    s[i] = 'b';
                }
 
                // if the alphabet is not z
                else {
 
                    // replace the element with
                    // next immediate alphabet
                    s[i] = (char)(s[i] + 1);
 
                    // if next immediate alphabet is vowel,
                    // than take next 2nd immediate alphabet
                    // (since no two vowels occurs
                    // consecutively in alphabets) hence no
                    // further checking is required
                    if (isVowel(s[i])) {
                        s[i] = (char)(s[i] + 1);
                    }
                }
            }
        }
        return String.valueOf(s);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        String s = "geeksforgeeks";
        System.out.println(
            replaceConsonants(s.toCharArray()));
    }
}
 
// This code is contributed by Rajput-Ji

Python3

# Python3 program of above approach
 
# Function to check if a character is
# vowel or not
 
 
def isVowel(ch):
 
    if (ch != 'a' and ch != 'e' and
        ch != 'i' and ch != 'o' and
            ch != 'u'):
        return False
 
    return True
 
# Function that replaces consonant with
# next immediate consonant alphabetically
 
 
def replaceConsonants(s):
 
    # Start traversing the string
    for i in range(len(s)):
        if (isVowel(s[i]) == False):
 
            # if character is z,
            # than replace it with character b
            if (s[i] == 'z'):
                s[i] = 'b'
 
            # if the alphabet is not z
            else:
 
                # replace the element with
                # next immediate alphabet
                s[i] = chr(ord(s[i]) + 1)
 
                # if next immediate alphabet is vowel,
                # than take next 2nd immediate alphabet
                # (since no two vowels occurs consecutively
                # in alphabets) hence no further
                # checking is required
                if (isVowel(s[i]) == True):
                    s[i] = chr(ord(s[i]) + 1)
 
    return ''.join(s)
 
 
# Driver code
s = "geeksforgeeks"
 
print(replaceConsonants(list(s)))
 
# This code is contributed by mits

C#

// C# program of above approach
using System;
 
class GFG {
 
    // Function to check if a character is
    // vowel or not
    static bool isVowel(char ch)
    {
        if (ch != 'a' && ch != 'e' && ch != 'i' && ch != 'o'
            && ch != 'u') {
            return false;
        }
        return true;
    }
 
    // Function that replaces consonant with
    // next immediate consonant alphabetically
    static String replaceConsonants(char[] s)
    {
 
        // Start traversing the string
        for (int i = 0; i < s.Length; i++) {
            if (!isVowel(s[i])) {
 
                // if character is z,
                // than replace it with character b
                if (s[i] == 'z') {
                    s[i] = 'b';
                }
 
                // if the alphabet is not z
                else {
 
                    // replace the element with
                    // next immediate alphabet
                    s[i] = (char)(s[i] + 1);
 
                    // if next immediate alphabet is vowel,
                    // than take next 2nd immediate alphabet
                    // (since no two vowels occurs
                    // consecutively in alphabets) hence no
                    // further checking is required
                    if (isVowel(s[i])) {
                        s[i] = (char)(s[i] + 1);
                    }
                }
            }
        }
        return String.Join("", s);
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        String s = "geeksforgeeks";
        Console.WriteLine(
            replaceConsonants(s.ToCharArray()));
    }
}
 
// This code is contributed by
// 29AjayKumar

PHP

<?php
// PHP program of above approach
 
// Function to check if a character is
// vowel or not
function isVowel($ch)
{
    if ($ch != 'a' && $ch != 'e' &&
        $ch != 'i' && $ch != 'o' &&
        $ch != 'u')
        return false;
 
    return true;
}
 
// Function that replaces consonant with
// next immediate consonant alphabetically
function replaceConsonants($s)
{
    // Start traversing the string
    for ($i = 0; $i < strlen($s); $i++)
    {
        if (!isVowel($s[$i]))
        {
 
            // if character is z,
            // than replace it with character b
            if ($s[$i] == 'z')
                $s[$i] = 'b';
 
            // if the alphabet is not z
            else
            {
 
                // replace the element with
                // next immediate alphabet
                $s[$i] = chr(ord($s[$i]) + 1);
 
                // if next immediate alphabet is vowel,
                // than take next 2nd immediate alphabet
                // (since no two vowels occurs consecutively
                // in alphabets) hence no further
                // checking is required
                if (isVowel($s[$i]))
                    $s[$i] = chr(ord($s[$i]) + 1);
            }
        }
    }
    return $s;
}
 
// Driver code
$s = "geeksforgeeks";
 
echo replaceConsonants($s);
 
// This code is contributed by mits
?>

Javascript

<script>
 
// Javascript program of above approach
 
// Function to check if a character is
// vowel or not
function isVowel(ch)
{
    if (ch != 'a' && ch != 'e' && ch != 'i'
                     && ch != 'o' && ch != 'u')
        return false;
 
    return true;
}
 
// Function that replaces consonant with
// next immediate consonant alphabetically
function replaceConsonants(s)
{
    // Start traversing the string
    for (var i = 0; i < s.length; i++) {
 
        if (!isVowel(s[i])) {
 
            // if character is z,
            // than replace it with character b
            if (s[i] == 'z')
                s[i] = 'b';
 
            // if the alphabet is not z
            else {
 
                // replace the element with
                // next immediate alphabet
                s[i] = String.fromCharCode(s[i].charCodeAt(0) + 1);
 
                // if next immediate alphabet is vowel,
                // than take next 2nd immediate alphabet
                // (since no two vowels occurs consecutively
                // in alphabets) hence no further
                // checking is required
                if (isVowel(s[i]))
                    s[i] = String.fromCharCode(s[i].charCodeAt(0) + 1);
            }
        }
    }
 
    return s.join('');
}
 
// Driver code
var s = "geeksforgeeks".split('');
document.write( replaceConsonants(s));
 
 
</script>
Producción: 

heeltgosheelt

 

Complejidad de tiempo: O(n), donde n es el tamaño de la string s 
Espacio auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por Shashank_Sharma 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 *