📜  从字符串中删除元音的程序

📅  最后修改于: 2021-05-07 00:00:02             🧑  作者: Mango

给定一个字符串,从字符串中删除元音和打印字符串没有元音。

例子:

Input : welcome to geeksforgeeks
Output : wlcm t gksfrgks

Input : what is your name ?
Output : wht s yr nm ?

设计了一个循环,循环通过由该字符串的字符组成的列表,删除元音,然后将其连接。

C++
// C++ program to remove vowels from a String
#include 
using namespace std;
  
string remVowel(string str) 
{
    vector vowels = {'a', 'e', 'i', 'o', 'u',
                           'A', 'E', 'I', 'O', 'U'};
      
    for (int i = 0; i < str.length(); i++) 
    {
        if (find(vowels.begin(), vowels.end(),
                      str[i]) != vowels.end()) 
        {
            str = str.replace(i, 1, "");
            i -= 2;
        }
    }
    return str;
}
  
// Driver Code
int main() 
{
    string str = "GeeeksforGeeks - A Computer"
                 " Science Portal for Geeks";
    cout << remVowel(str) << endl;
  
    return 0;
}
  
// This code is contributed by
// sanjeev2552


Java
// Java program to remove vowels from a String
  
import java.util.Arrays;
import java.util.List;
  
class Test
{    
    static String remVowel(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(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(remVowel(str));
    }
}


Python
# Python program to remove vowels from a string
# Function to remove vowels
def rem_vowel(string):
    vowels = ('a', 'e', 'i', 'o', 'u') 
    for x in string.lower():
        if x in vowels:
            string = string.replace(x, "")
              
    # Print string without vowels
    print(string)
  
# Driver program
string = "GeeksforGeeks - A Computer Science Portal for Geeks"
rem_vowel(string)


C#
// C# program to remove vowels from a String
using System;
using System.Text;
using System.Linq;
using System.Collections.Generic;
public class Test
{    
    static String remVowel(String str)
    {
         char []vowels = {'a', 'e', 'i', 'o', 'u','A','E','I','O','U'};
            
         List al = vowels.OfType().ToList();;
            
         StringBuilder sb = new StringBuilder(str);
            
         for (int i = 0; i < sb.Length; i++) {
               
             if(al.Contains(sb[i])){
                sb.Replace(sb[i].ToString(), "") ;
                i--;
             }
        }
            
           
        return sb.ToString();
    }
    // Driver method to test the above function
    public static void Main() 
    {
        String str = "GeeeksforGeeks - A Computer Science Portal for Geeks";
           
        Console.Write(remVowel(str));
    }
}
//This code is contributed by Rajput-Ji


C++
// C++ program to remove vowels from a String
#include 
using namespace std;
  
string remVowel(string str)
{
    regex r("[aeiouAEIOU]"); 
      
    return regex_replace(str, r, "");
}
  
// Driver Code
int main() 
{
    string str = "GeeeksforGeeks - A Computer Science Portal for Geeks";     
    cout << (remVowel(str));
    return 0;
}
  
// This code is contributed by Arnab Kundu


Java
// Java program to remove vowels from a String
import java.util.Arrays;
import java.util.List;
  
class GFG
{    
    static String remVowel(String str)
    {
         return str.replaceAll("[aeiouAEIOU]", "");
    }
      
    // Driver Code
    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 vowels from a String
using System;
using System.Text.RegularExpressions;
  
class GFG
{ 
    static String remVowel(String str)
    {
        str = Regex.Replace(str, "[aeiouAEIOU]", "");
        return str;
    }
      
    // Driver code
    public static void Main() 
    {
        String str = "GeeeksforGeeks - A Computer Science Portal for Geeks";     
        Console.WriteLine(remVowel(str));
    }
}
  
//This code is contributed by 29AjayKumar


Python
# Python program to remove vowels from a string
# Function to remove vowels
  
# import the module for regular expression (re)
import re
def rem_vowel(string):
    return (re.sub("[aeiouAEIOU]","",string))            
  
# Driver program
string = "GeeksforGeeks - A Computer Science Portal for Geeks"
print rem_vowel(string)


输出:

GksfrGks - Cmptr Scnc Prtl fr Gks

我们可以使用正则表达式来改进上述解决方案。

C++

// C++ program to remove vowels from a String
#include 
using namespace std;
  
string remVowel(string str)
{
    regex r("[aeiouAEIOU]"); 
      
    return regex_replace(str, r, "");
}
  
// Driver Code
int main() 
{
    string str = "GeeeksforGeeks - A Computer Science Portal for Geeks";     
    cout << (remVowel(str));
    return 0;
}
  
// This code is contributed by Arnab Kundu

Java

// Java program to remove vowels from a String
import java.util.Arrays;
import java.util.List;
  
class GFG
{    
    static String remVowel(String str)
    {
         return str.replaceAll("[aeiouAEIOU]", "");
    }
      
    // Driver Code
    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 vowels from a String
using System;
using System.Text.RegularExpressions;
  
class GFG
{ 
    static String remVowel(String str)
    {
        str = Regex.Replace(str, "[aeiouAEIOU]", "");
        return str;
    }
      
    // Driver code
    public static void Main() 
    {
        String str = "GeeeksforGeeks - A Computer Science Portal for Geeks";     
        Console.WriteLine(remVowel(str));
    }
}
  
//This code is contributed by 29AjayKumar

Python

# Python program to remove vowels from a string
# Function to remove vowels
  
# import the module for regular expression (re)
import re
def rem_vowel(string):
    return (re.sub("[aeiouAEIOU]","",string))            
  
# Driver program
string = "GeeksforGeeks - A Computer Science Portal for Geeks"
print rem_vowel(string)

输出:

GksfrGks -  Cmptr Scnc Prtl fr Gks