First X vowels from a string
Last Updated :
12 Sep, 2022
Given a string str and an integer X, the task is to find and print the first X vowels from str. If the 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
The total vowel count is 2 i.e ‘o’ and ‘o’
Approach: Traverse the string character by character and check if the current character is a vowel. If the 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++
#include <bits/stdc++.h>
using namespace std;
bool isVowel( char c)
{
c = tolower (c);
if (c == 'a' || c == 'e' || c == 'i'
|| c == 'o' || c == 'u' )
return true ;
return false ;
}
string firstXvowels(string s, int x)
{
string result = "" ;
for ( int i = 0; i < s.length(); i++) {
if (isVowel(s[i]))
result += s[i];
if (result.length() == x) {
return result;
}
}
return "-1" ;
}
int main()
{
string str = "GeeksForGeeks" ;
int x = 3;
cout << firstXvowels(str, x);
return 0;
}
|
Java
public class GFG{
static boolean isVowel( char c)
{
c = Character.toLowerCase(c) ;
if (c == 'a' || c == 'e' || c == 'i'
|| c == 'o' || c == 'u' )
return true ;
return false ;
}
static String firstXvowels(String s, int x)
{
String result = "" ;
for ( int i = 0 ; i < s.length(); i++) {
if (isVowel(s.charAt(i)))
result += s.charAt(i);
if (result.length() == x) {
return result;
}
}
return "-1" ;
}
public static void main(String []args)
{
String str = "GeeksForGeeks" ;
int x = 3 ;
System.out.println(firstXvowels(str, x)) ;
}
}
|
Python3
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
def firstXvowels(s, x):
result = ""
for i in range ( 0 , len (s), 1 ):
if (isVowel(s[i])):
result + = s[i]
if ( len (result) = = x):
return result
return "-1"
if __name__ = = '__main__' :
str = "GeeksForGeeks"
x = 3
print (firstXvowels( str , x))
|
C#
using System;
class GFG
{
static bool isVowel( char c)
{
c = Char.ToLower(c) ;
if (c == 'a' || c == 'e' ||
c == 'i' || c == 'o' ||
c == 'u' )
return true ;
return false ;
}
static string firstXvowels( string s, int x)
{
string result = "" ;
for ( int i = 0; i < s.Length; i++)
{
if (isVowel(s[i]))
result += s[i];
if (result.Length == x)
{
return result;
}
}
return "-1" ;
}
public static void Main()
{
string str = "GeeksForGeeks" ;
int x = 3;
Console.WriteLine(firstXvowels(str, x)) ;
}
}
|
PHP
<?php
function isVowel( $c )
{
$c = strtolower ( $c );
if ( $c == 'a' || $c == 'e' ||
$c == 'i' || $c == 'o' || $c == 'u' )
return true;
return false;
}
function firstXvowels( $s , $x )
{
$result = "" ;
for ( $i = 0; $i < strlen ( $s ); $i ++)
{
if (isVowel( $s [ $i ]))
$result .= $s [ $i ];
if ( strlen ( $result ) == $x )
{
return $result ;
}
}
return "-1" ;
}
$str = "GeeksForGeeks" ;
$x = 3;
echo firstXvowels( $str , $x );
?>
|
Javascript
<script>
function isVowel(c)
{
c = (c.toLowerCase());
if (c == 'a' || c == 'e' || c == 'i'
|| c == 'o' || c == 'u' )
return true ;
return false ;
}
function firstXvowels(s, x)
{
var result = "" ;
for ( var i = 0; i < s.length; i++) {
if (isVowel(s[i]))
result += s[i];
if (result.length == x) {
return result;
}
}
return "-1" ;
}
var str = "GeeksForGeeks" ;
var x = 3;
document.write( firstXvowels(str, x));
</script>
|
Complexity Analysis:
- Time Complexity: O(n) Here, n is the length of the string.
- Auxiliary Space: O(x) Where x is the no of vowels in the string
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...