📌  相关文章
📜  在字符串中按字母顺序将辅音替换为下一个直接辅音

📅  最后修改于: 2022-05-13 01:57:08.946000             🧑  作者: Mango

在字符串中按字母顺序将辅音替换为下一个直接辅音

给定一个包含小写英文字母的字符串。任务是用英语字母表中的下一个直接辅音替换每个辅音。
假设我们必须替换字符a   ,它将被替换为b   .另一个例子,假设我们必须替换字符d   , 下一个直接辅音是f   , 因此d   将被替换为f   .
注意:如果字符是'z',则在英文字母中循环查找下一个辅音,即将它替换为'b'。
例子

Input : str = "geeksforgeeks"
Output : heeltgosheelt

Input : str = "gfg"
Output : hgh

方法:

  • 从左到右迭代字符串元素。
  • 如果字符串元素是辅音,则检查该元素的下一个直接字母表。
  • 如果下一个直接字母是辅音,则将其替换为这个字母。如果它是元音,则用第二个直接字母替换字符串元素,因为英文字母中没有连续的元音。

下面是上述程序的实现:

C++
// C++ program of above approach
#include 
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


Javascript


输出:
heeltgosheelt