📜  从字符串中删除连续的元音

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

从字符串中删除连续的元音

给定一个由小写字母组成的字符串s,我们需要从字符串中删除连续的元音
注意:句子不应包含两个连续的元音( a、e、i、o、u )。
例子 :

Input: geeks for geeks
Output: geks for geks

Input : your article is in queue 
Output : your article is in qu

方法:使用循环迭代字符串并检查给定句子中元音的重复性,如果找到连续的元音,则删除元音直到下一个辅音并打印更新的字符串。

C++
// C++ program for printing sentence
// without repetitive vowels
#include 
using namespace std;
 
// function which returns True or False
// for occurrence of a vowel
bool is_vow(char c)
{
    // this compares vowel with
    // character 'c'
    return (c == 'a') || (c == 'e') ||
           (c == 'i') || (c == 'o') ||
           (c == 'u');
}
 
// function to print resultant string
void removeVowels(string str)
{
    // print 1st character
    printf("%c", str[0]);
 
    // loop to check for each character
    for (int i = 1; str[i]; i++)
 
        // comparison of consecutive characters
        if ((!is_vow(str[i - 1])) ||
            (!is_vow(str[i])))
            printf("%c", str[i]);
}
 
// Driver Code
int main()
{
    char str[] = " geeks for geeks";
    removeVowels(str);
}
 
// This code is contributed by Abhinav96


Java
// Java program for printing sentence
// without repetitive vowels
import java.io.*;
import java.util.*;
import java.lang.*;
 
class GFG
{
    // function which returns
    // True or False for
    // occurrence of a vowel
    static boolean is_vow(char c)
    {
        // this compares vowel
        // with character 'c'
        return (c == 'a') || (c == 'e') ||
               (c == 'i') || (c == 'o') ||
               (c == 'u');
    }
     
    // function to print
    // resultant string
    static void removeVowels(String str)
    {
        // print 1st character
        System.out.print(str.charAt(0));
     
        // loop to check for
        // each character
        for (int i = 1;
                 i < str.length(); i++)
     
            // comparison of
            // consecutive characters
            if ((!is_vow(str.charAt(i - 1))) ||
                (!is_vow(str.charAt(i))))
                System.out.print(str.charAt(i));
    }
     
    // Driver Code
    public static void main(String[] args)
    {
        String str = "geeks for geeks";
        removeVowels(str);
    }
}


Python3
# Python3 implementation for printing
# sentence without repetitive vowels
 
# function which returns True or False
# for occurrence of a vowel
def is_vow(c):
 
    # this compares vowel with
    # character 'c'
    return ((c == 'a') or (c == 'e') or
            (c == 'i') or (c == 'o') or
            (c == 'u'));
 
# function to print resultant string
def removeVowels(str):
 
    # print 1st character
    print(str[0], end = "");
 
    # loop to check for each character
    for i in range(1,len(str)):
 
        # comparison of consecutive
        # characters
        if ((is_vow(str[i - 1]) != True) or
            (is_vow(str[i]) != True)):
             
            print(str[i], end = "");
 
# Driver code
str= " geeks for geeks";
removeVowels(str);
 
# This code is contributed by mits


C#
// C# program for printing sentence
// without repetitive vowels
using System;
 
class GFG
{
    // function which returns
    // True or False for
    // occurrence of a vowel
    static bool is_vow(char c)
    {
        // this compares vowel
        // with character 'c'
        return (c == 'a') || (c == 'e') ||
               (c == 'i') || (c == 'o') ||
               (c == 'u');
    }
     
    // function to print
    // resultant string
    static void removeVowels(string str)
    {
        // print 1st character
        Console.Write(str[0]);
     
        // loop to check for
        // each character
        for (int i = 1; i < str.Length; i++)
     
            // comparison of
            // consecutive characters
            if ((!is_vow(str[i - 1])) ||
                (!is_vow(str[i])))
                Console.Write(str[i]);
    }
     
    // Driver Code
    static void Main()
    {
        string str = "geeks for geeks";
        removeVowels(str);
    }
}
 
// This code is contributed
// by Manish Shaw(manishshaw1)


PHP


Javascript


输出 :
geks for geks