📌  相关文章
📜  从字符串中删除辅音的程序

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

从字符串中删除辅音的程序

给定一个字符串str            ,任务是从字符串中删除所有辅音,然后打印字符串。

例子:

方法:遍历字符串的所有字符,如果该字符是辅音,则将其从最终答案中删除。

下面是上述方法的实现:

Java
// Java program to remove consonants from a String
import java.util.Arrays;
import java.util.List;
 
class Test {
 
    // function that returns true
    // if the character is an alphabet
    static boolean isAlphabet(char ch)
    {
        if (ch >= 'a' && ch <= 'z')
            return true;
        if (ch >= 'A' && ch <= 'Z')
            return true;
        return false;
    }
 
    // function to return the string after
    // removing all the consonants from it
    static String remConsonants(String str)
    {
        Character vowels[]
            = { 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U' };
 
        List al = Arrays.asList(vowels);
 
        StringBuffer sb = new StringBuffer(str);
 
        for (int i = 0; i < sb.length(); i++) {
 
            if (isAlphabet(sb.charAt(i))
                && !al.contains(sb.charAt(i))) {
                sb.replace(i, i + 1, "");
                i--;
            }
        }
 
        return sb.toString();
    }
 
    // Driver method to test the above function
    public static void main(String[] args)
    {
        String str
            = "GeeeksforGeeks - A Computer Science Portal for Geeks";
 
        System.out.println(remConsonants(str));
    }
}


Python3
# Python3 program to remove consonants from a String
  
# function that returns true
# if the character is an alphabet
def isAlphabet(ch): 
    if (ch >= 'a' and ch <= 'z'):
        return True;
    if (ch >= 'A' and ch <= 'Z'):
        return True;
    return False;
 
# Function to return the string after
# removing all the consonants from it
def remConsonants(str):
    vowels = [ 'a', 'e', 'i', 'o', 'u',  'A', 'E', 'I', 'O', 'U' ]    
    sb = "";   
    for i in range(len(str)):   
        present = False;       
        for j in range(len(vowels)):   
            if (str[i] == vowels[j]):           
                present = True;
                break;        
        if (not isAlphabet(str[i]) or present ):   
            sb += str[i];
    return sb;
 
# Driver code
if __name__=='__main__':   
    str = "GeeeksforGeeks - A Computer Science Portal for Geeks";
    print(remConsonants(str));
 
    # This code is contributed by pratham76


C#
// C# program to remove consonants from a String
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
 
class GFG{
  
// Function that returns true
// if the character is an alphabet
static bool isAlphabet(char ch)
{
    if (ch >= 'a' && ch <= 'z')
        return true;
    if (ch >= 'A' && ch <= 'Z')
        return true;
         
    return false;
}
 
// Function to return the string after
// removing all the consonants from it
static string remConsonants(string str)
{
    char []vowels = { 'a', 'e', 'i', 'o', 'u',
                      'A', 'E', 'I', 'O', 'U' };
     
    string sb = "";
 
    for(int i = 0; i < str.Length; i++)
    {
        bool present = false;
        for(int j = 0; j < vowels.Length; j++)
        {
            if (str[i] == vowels[j])
            {
                present = true;
                break;
            }
        }
         
        if (!isAlphabet(str[i]) || present )
        {
            sb += str[i];
        }
    }
    return sb;
}
 
// Driver code
public static void Main(string[] args)
{
    string str = "GeeeksforGeeks - A Computer Science Portal for Geeks";
 
    Console.Write(remConsonants(str));
}
}
 
// This code is contributed by rutvik_56


Javascript


Java
// Java program to remove consonants from a String
 
class Test
{   
    static String remVowel(String str)
    {
         return str.replaceAll("[BCDFGHJKLMNPQRSTVXZbcdfghjklmnpqrstvxz]", "");
    }
     
    // Driver method to test the above function
    public static void main(String[] args)
    {
        String str = "GeeeksforGeeks - A Computer Science Portal for Geeks";
         
        System.out.println(remVowel(str));
    }
}


C#
// C# program to remove consonants from a String
using System;
using System.Text.RegularExpressions;
 
class GFG
{
    static String remVowel(String str)
    {
        return Regex.Replace(str, "[BCDFGHJKLMNPQRSTVXZbcdfghjklmnpqrstvxz]", "");
    }
     
    // Driver code
    public static void Main()
    {
        String str = "GeeeksforGeeks - A Computer Science Portal for Geeks";
         
        Console.WriteLine(remVowel(str));
    }
}
 
// This code is contributed by Rajput-Ji


输出:
eeeoee - A oue iee oa o ee

下面是另一个在Java中使用正则表达式的程序

Java

// Java program to remove consonants from a String
 
class Test
{   
    static String remVowel(String str)
    {
         return str.replaceAll("[BCDFGHJKLMNPQRSTVXZbcdfghjklmnpqrstvxz]", "");
    }
     
    // Driver method to test the above function
    public static void main(String[] args)
    {
        String str = "GeeeksforGeeks - A Computer Science Portal for Geeks";
         
        System.out.println(remVowel(str));
    }
}

C#

// C# program to remove consonants from a String
using System;
using System.Text.RegularExpressions;
 
class GFG
{
    static String remVowel(String str)
    {
        return Regex.Replace(str, "[BCDFGHJKLMNPQRSTVXZbcdfghjklmnpqrstvxz]", "");
    }
     
    // Driver code
    public static void Main()
    {
        String str = "GeeeksforGeeks - A Computer Science Portal for Geeks";
         
        Console.WriteLine(remVowel(str));
    }
}
 
// This code is contributed by Rajput-Ji
输出:
eeeoee - A oue iee oa o ee

时间复杂度: O(n),其中 n 是字符串的长度