Playing with string consisting of vowels

Playing with String Consisting of vowels

In this page we will discuss the program for playing with string consisting of vowels, the program will then remove vowels a, e, i, o, u (in lower or upper case) from the string. If two vowels are consecutive then we will not removing consecutive vowels, we have to ignore them.
Playing with string consisting of vowels

Method Discussed :

  • Method 1 : Brute Force Approach.
  • Method 2 : By transforming the character in uppercase/lower case.
  • Method 3 : Without using extra space.

Method 1 :

  • Declare a temporary string res to store the required result.
  • Iterate over the string.
  • Check for the every character, if it is a vowel,
  • Then check for next character, if it is vowel then add both character to required string.
  • Otherwise skip that character.
  • At last return string res.

Time and Space Complexity :

  • Time-Complexity : O(n), where n is the length of the input string
  • Space Complexity : O(n) where n is the length of the input string
Run
//write a program that will take one string as input. the program will then remove vowels a, e, i, o, u
//from the string if there are two or more than two vowels than do not removing consecutive vowels in C
#include <stdio.h>
#include <string.h>

int main() {
    // Initializing variable.
    char s[100], res[100];
    int i, j, len = 0, k=0;
    
    // gets(str);
    scanf("%%[^\n]s", s);

    len = strlen(s);
    // Accepting input.

    for(i=0; i<len-1; i++){ 
       
       char temp=s[i], temp2=s[i+1];
       if(s[i] == 'a' or s[i] == 'e' or s[i] == 'i' or s[i] == 'o' or s[i] == 'u' or
           s[i] == 'A' or s[i] == 'E' or s[i] == 'I' or s[i] == 'O' or s[i] == 'U'){
              
              if(s[i+1] == 'a' or s[i+1] == 'e' or s[i+1] == 'i' or s[i+1] == 'o' or s[i+1] == 'u' or 
                s[i+1] == 'A' or s[i+1] == 'E' or s[i+1] == 'I' or s[i+1] == 'O' or s[i+1] == 'U'){ 
                 
                 res[k++] =  s[i];
                 res[k++] = s[i+1];
                 i++; 
            } 
           else continue; 
        } 
        else 
            res[k++] = s[i];
      } 
     // for handling last character 
     char temp = s[i]; 
     if(!(temp == 'a' or temp == 'e' or temp == 'i' or temp == 'o' or temp == 'u' 
        or temp == 'A' or temp == 'E' or temp == 'I' or temp == 'O' or temp == 'U')) 
        res[k++] = temp; 
        res[k] = '\n';
        
   printf("%s", res);
   return 0;
}
Run
//write a program that will take one string as input. the program will then remove vowels a, e, i, o, u
//from the string if there are two or more than two vowels than do not removing consecutive vowels.
#include<bits/stdc++.h>
using namespace std;

string solution(string s){
    
    int n = s.size(), i=0;
    string res="";
    
    for(i=0; i<n-1; i++){ 
        char temp=s[i], temp2=s[i+1];
       if(s[i] == 'a' or s[i] == 'e' or s[i] == 'i' or s[i] == 'o' or s[i] == 'u' or
           s[i] == 'A' or s[i] == 'E' or s[i] == 'I' or s[i] == 'O' or s[i] == 'U'){
              if(s[i+1] == 'a' or s[i+1] == 'e' or s[i+1] == 'i' or s[i+1] == 'o' or s[i+1] == 'u' or 
                s[i+1] == 'A' or s[i+1] == 'E' or s[i+1] == 'I' or s[i+1] == 'O' or s[i+1] == 'U'){ 
                 res = res + s[i] + s[i+1];
                 i++; 
            } 
           else continue; 
        } 
        else 
            res += s[i];
      } 
     // for handling last character 
     char temp = s[i]; 
     if(!(temp == 'a' or temp == 'e' or temp == 'i' or temp == 'o' or temp == 'u' 
        or temp == 'A' or temp == 'E' or temp == 'I' or temp == 'O' or temp == 'U')) 
        res += temp; 
        return res; 
} 

int main(){
  string s; 
  getline(cin, s); 
  string res = solution(s);
  cout<<res;
}
Run
string = "Vaibhav"
vowels = "aeiouAEIOU"
output = ""
for idx in range(len(string)-1):
  if idx == 0 and string[idx] not in vowels:
    output+=string[idx]
  elif (string[idx-1] in vowels and string[idx] in vowels) or (string[idx+1] in vowels and string[idx] in vowels):
    output+=string[idx]
  elif string[idx] not in vowels:
    output+=string[idx]

if string[idx+1] in vowels and string[idx] in vowels:
  output+=string[idx+1]   
else:
  output+=string[idx+1]
print(output)

Output

Welcome to PrepInsta
Wlcm t Prpnst

Put this on Chair
Pt ths n Chr

Method 2 :

In this method we will convert the ith character of the string in either lowercase or uppercase, then we will remove all the vowels except consecutive occurring vowels.

Time and Space Complexity :

  • Time-Complexity : O(n), where n is the length of the input string
  • Space Complexity : O(n) where n is the length of the input string
Run
//write a program that will take one string as input. the program will then remove vowels a, e, i, o, u
//from the string if there are two or more than two vowels than do not removing consecutive vowels in C
#include <ctype.h>
#include <stdio.h>
#include <string.h>

int main() {
    // Initializing variable.
    char s[100], res[100];
    int i, j, len = 0, k=0;
    
    // gets(s);
    scanf("%[^\n]s", s);

    len = strlen(s);

    for(i=0; i<len-1; i++){ 
       
       char temp = tolower(s[i]), temp2 = tolower(s[i+1]);
       if(s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u'){
              
              if(s[i+1] == 'a' || s[i+1] == 'e' || s[i+1] == 'i' || s[i+1] == 'o' || s[i+1] == 'u'){ 
                 
                 res[k++] =  s[i];
                 res[k++] = s[i+1];
                 i++; 
            } 
           else continue; 
        } 
        else 
            res[k++] = s[i];
      } 
     // for handling last character 
     char temp = tolower(s[i]); 
     if(!(temp == 'a' || temp == 'e' || temp == 'i' || temp == 'o' || temp == 'u')) 
        res[k++] = temp; 
        res[k] = '\n';
        
   printf("%s", res);
   return 0;
}
Run
//write a program that will take one string as input. the program will then remove vowels a, e, i, o, u
//from the string if there are two or more than two vowels than do not removing consecutive vowels in C++
#include<bits/stdc++.h>
using namespace std;

string solution(string s){
    
    int n = s.size(), i=0;
    string res="";
    
    for(i=0; i< n-1; i++){
        
        char temp= tolower(s[i]), temp2=tolower(s[i+1]);
        
        if(temp == 'a' or temp == 'e' or temp == 'i' or temp == 'o' or temp == 'u'){
            

            if(temp2 == 'a' or temp2 == 'e' or temp2 == 'i' or temp2 == 'o' or temp2 == 'u'){
                res = res + s[i] + s[i+1];
                i++;
            }
            
            else continue;
        }
        
       else res += s[i];
    }
    
    // for handling last character
    
    char temp = tolower(s[i]);
    if(!(temp == 'a' or temp == 'e' or temp == 'i' or temp == 'o' or temp == 'u'))
    res += temp;
        
    
    return res;
}



int main(){
  string s;
  getline(cin, s);
  
  string res = solution(s);
  cout<<res;
}
Run
original_string = "Vaibhav"
def Uppercase(original_string):
  string = ''
  for i in original_string:
    if ord(i)<=122 and ord(i)>=97:
      string+=chr(ord(i)-32)
    else:
      string+=i
  return string
string = Uppercase(original_string)
vowels = "AEIOU"
output = ""
for idx in range(len(string)-1):
  if idx == 0 and string[idx] not in vowels:
    output+=string[idx]
  elif (string[idx-1] in vowels and string[idx] in vowels) or (string[idx+1] in vowels and string[idx] in vowels):
    output+=string[idx]
  elif string[idx] not in vowels:
    output+=string[idx]

if string[idx+1] in vowels and string[idx] in vowels:
  output+=string[idx+1]   
else:
  output+=string[idx+1]
print(output.title())

Output

Welcome to PrepInsta
Wlcm t Prpnst

Put this on Chair
Pt ths n Chr

Method 3 :

In this method we will erase the required elements from the actual string.

Time and Space Complexity :

  • Time-Complexity : O(n), where n is the length of the input string
  • Space Complexity : O(1)
Run
//write a program that will take one string as input. the program will then remove vowels a, e, i, o, u
//from the string if there are two or more than two vowels than do not removing consecutive vowels in C++
#include<bits/stdc++.h>
using namespace std;

string solution(string s){
    
    int n = s.size(), i=0;
    
    for(i=0; i<n-1; i++){
        
        char temp= tolower(s[i]), temp2=tolower(s[i+1]);
        
        if(temp == 'a' or temp == 'e' or temp == 'i' or temp == 'o' or temp == 'u'){
            

            if(temp2 == 'a' or temp2 == 'e' or temp2 == 'i' or temp2 == 'o' or temp2 == 'u'){
                i++;
            }
            
            else {
                s.erase(i, 1);
            }
            
        }
        
        
       else continue;
    }
    
    // for handling last character
    
    char temp = tolower(s[i]);
    if((temp == 'a' or temp == 'e' or temp == 'i' or temp == 'o' or temp == 'u'))
     s.erase(i, 1);
        
    
    return s;
}



int main(){
  string s;
  getline(cin, s);
  
  string res = solution(s);
  cout<<res;
}

Output

Welcome to PrepInsta
Wlcm t Prpnst

Put this on Chair
Pt ths n Chr

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription