# First X vowels from a string

Given a string str and an integer X, the task is to find and print first X vowels from str. If total vowels in str is < X then print -1.

Examples:

Input: str = “GeeksForGeeks”, X = 3
Output: eeo
‘e’, ‘e’ and ‘o’ are the first three vowels in the given string.

Input: str = “softcopy”, X = 5
Output: -1
Total vowel count is 2 i.e ‘o’ and ‘o’

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Traverse the string character by character and check if the current character is a vowel. If current character is a vowel then concatenate it to the resultant string, result. If at any point the length of the resultant string becomes X then print the string result else print -1 in the end.

Below is the implementation of the above approach:

## 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

 ` `

Output:

```eeo
```

Time Complexity: O(n) Here, n is the length of string.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.