📜  字符串的前X个元音

📅  最后修改于: 2021-05-28 04:28:30             🧑  作者: Mango

给定一个字符串str和一个整数X ,任务是从str找到并打印第一个X元音。如果str中的元音总数小于X,则打印-1

例子:

做法:遍历字符串逐个,并检查当前字符是元音。如果当前字符是元音,则将其连接到结果字符串,结果。如果在任何时候,结果字符串的长度变为X,则打印该字符串结果,否则最后打印-1

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to check if a character is vowel
bool isVowel(char c)
{
    c = tolower(c);
    if (c == 'a' || c == 'e' || c == 'i'
        || c == 'o' || c == 'u')
        return true;
    return false;
}
  
// Function to return first X vowels
string firstXvowels(string s, int x)
{
    // String to store first X vowels
    string result = "";
    for (int i = 0; i < s.length(); i++) {
  
        // If s[i] is a vowel then
        // append it to the result
        if (isVowel(s[i]))
            result += s[i];
  
        // If the desired length is reached
        if (result.length() == x) {
            return result;
        }
    }
  
    // If total vowels are < X
    return "-1";
}
  
// Driver code
int main()
{
    string str = "GeeksForGeeks";
    int x = 3;
  
    cout << firstXvowels(str, x);
  
    return 0;
}


Java
// Java implementation of the approach 
  
public class GFG{
      
    // Function to check if a character is vowel 
    static boolean isVowel(char c) 
    { 
        c = Character.toLowerCase(c) ;
        if (c == 'a' || c == 'e' || c == 'i'
            || c == 'o' || c == 'u') 
            return true; 
        return false; 
    } 
      
    // Function to return first X vowels 
    static String firstXvowels(String s, int x) 
    { 
        // String to store first X vowels 
        String result = ""; 
        for (int i = 0; i < s.length(); i++) { 
      
            // If s[i] is a vowel then 
            // append it to the result 
            if (isVowel(s.charAt(i))) 
                result += s.charAt(i); 
      
            // If the desired length is reached 
            if (result.length() == x) { 
                return result; 
            } 
        } 
      
        // If total vowels are < X 
        return "-1"; 
    } 
      
    // Driver code 
    public static void main(String []args)
    { 
        String str = "GeeksForGeeks"; 
        int x = 3; 
      
        System.out.println(firstXvowels(str, x)) ; 
    } 
    // This code is contributed by Ryuga
    }


Python3
# Python 3 implementation of the approach
  
# Function to check if a character is vowel
def isVowel(c):
    c = c.lower()
    if (c == 'a' or c == 'e' or 
        c == 'i' or c == 'o' or c == 'u'):
        return True
    return False
  
# Function to return first X vowels
def firstXvowels(s, x):
      
    # String to store first X vowels
    result = ""
    for i in range(0, len(s), 1):
          
        # If s[i] is a vowel then
        # append it to the result
        if (isVowel(s[i])):
            result += s[i]
  
        # If the desired length is reached
        if (len(result) == x):
            return result
      
    # If total vowels are < X
    return "-1"
  
# Driver code
if __name__ == '__main__':
    str = "GeeksForGeeks"
    x = 3
  
    print(firstXvowels(str, x))
  
# This code is implemented by
# Surendra_Gangwar


C#
// C# implementation of the approach 
using System;
  
class GFG
{
  
// Function to check if a 
// character is vowel 
static bool isVowel(char c) 
{ 
    c = Char.ToLower(c) ;
    if (c == 'a' || c == 'e' || 
        c == 'i' || c == 'o' || 
        c == 'u') 
        return true; 
    return false; 
} 
  
// Function to return first X vowels 
static string firstXvowels(string s, int x) 
{ 
    // String to store first X vowels 
    string result = ""; 
    for (int i = 0; i < s.Length; i++) 
    { 
  
        // If s[i] is a vowel then 
        // append it to the result 
        if (isVowel(s[i])) 
            result += s[i]; 
  
        // If the desired length is reached 
        if (result.Length == x) 
        { 
            return result; 
        } 
    } 
  
    // If total vowels are < X 
    return "-1"; 
} 
  
// Driver code 
public static void Main()
{ 
    string str = "GeeksForGeeks"; 
    int x = 3; 
  
    Console.WriteLine(firstXvowels(str, x)) ; 
} 
}
  
// This code is contributed 
// by Akanksha Rai


PHP


输出:
eeo

时间复杂度: O(n)在这里,n是字符串的长度。

想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。