Remove consonants from a string javascript

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given a string 

    Remove consonants from a string javascript
    , the task is to remove all the consonants from the string and then print the string.

    Examples:  

    Input: str= “Welcome to geeksforgeeks” 
    Output: eoe o eeoee

    Input: str= “What is your name?” 
    Output: a i ou ae? 

    Approach: Traverse all the characters of the string, if the character is a consonant then remove it from the final answer.

    Below is the implementation of the above approach:

    Java

    import java.util.Arrays;

    import java.util.List;

    class Test {

        static boolean isAlphabet(char ch)

        {

            if (ch >= 'a' && ch <= 'z')

                return true;

            if (ch >= 'A' && ch <= 'Z')

                return true;

            return false;

        }

        static String remConsonants(String str)

        {

            Character vowels[]

                = { 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U' };

            List<Character> al = Arrays.asList(vowels);

            StringBuffer sb = new StringBuffer(str);

            for (int i = 0; i < sb.length(); i++) {

                if (isAlphabet(sb.charAt(i))

                    && !al.contains(sb.charAt(i))) {

                    sb.replace(i, i + 1, "");

                    i--;

                }

            }

            return sb.toString();

        }

        public static void main(String[] args)

        {

            String str

                = "GeeeksforGeeks - A Computer Science Portal for Geeks";

            System.out.println(remConsonants(str));

        }

    }

    Python3

    def isAlphabet(ch): 

        if (ch >= 'a' and ch <= 'z'):

            return True;

        if (ch >= 'A' and ch <= 'Z'):

            return True;

        return False;

    def remConsonants(str):

        vowels = [ 'a', 'e', 'i', 'o', 'u''A', 'E', 'I', 'O', 'U' ]    

        sb = "";   

        for i in range(len(str)):   

            present = False;       

            for j in range(len(vowels)):   

                if (str[i] == vowels[j]):           

                    present = True;

                    break;        

            if (not isAlphabet(str[i]) or present ):   

                sb += str[i];

        return sb;

    if __name__=='__main__':   

        str = "GeeeksforGeeks - A Computer Science Portal for Geeks";

        print(remConsonants(str));

    C#

    using System;

    using System.Collections;

    using System.Collections.Generic;

    using System.Linq;

    class GFG{

    static bool isAlphabet(char ch)

    {

        if (ch >= 'a' && ch <= 'z')

            return true;

        if (ch >= 'A' && ch <= 'Z')

            return true;

        return false;

    }

    static string remConsonants(string str)

    {

        char []vowels = { 'a', 'e', 'i', 'o', 'u',

                          'A', 'E', 'I', 'O', 'U' };

        string sb = "";

        for(int i = 0; i < str.Length; i++)

        {

            bool present = false;

            for(int j = 0; j < vowels.Length; j++)

            {

                if (str[i] == vowels[j])

                {

                    present = true;

                    break;

                }

            }

            if (!isAlphabet(str[i]) || present )

            {

                sb += str[i];

            }

        }

        return sb;

    }

    public static void Main(string[] args)

    {

        string str = "GeeeksforGeeks - A Computer Science Portal for Geeks";

        Console.Write(remConsonants(str));

    }

    }

    Javascript

    <script>

          function isAlphabet(ch) {

            if (ch >= "a" && ch <= "z") return true;

            if (ch >= "A" && ch <= "Z") return true;

            return false;

          }

          function remConsonants(str) {

            var vowels =

            ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"];

            var sb = "";

            for (var i = 0; i < str.length; i++) {

              var present = false;

              for (var j = 0; j < vowels.length; j++) {

                if (str[i] === vowels[j]) {

                  present = true;

                  break;

                }

              }

              if (!isAlphabet(str[i]) || present) {

                sb += str[i];

              }

            }

            return sb;

          }

          var str =

          "GeeeksforGeeks - A Computer Science Portal for Geeks";

          document.write(remConsonants(str));

        </script>

    Output:

    eeeoee - A oue iee oa o ee

    Time Complexity: O(N)
    Auxiliary Space: O(N)

    Below is another program using Regular Expressions in Java 

    Java

    class Test

    {   

        static String remVowel(String str)

        {

             return str.replaceAll("[BCDFGHJKLMNPQRSTVXZbcdfghjklmnpqrstvxz]", "");

        }

        public static void main(String[] args)

        {

            String str = "GeeeksforGeeks - A Computer Science Portal for Geeks";

            System.out.println(remVowel(str));

        }

    }

    C#

    using System;

    using System.Text.RegularExpressions;

    class GFG

    {

        static String remVowel(String str)

        {

            return Regex.Replace(str, "[BCDFGHJKLMNPQRSTVXZbcdfghjklmnpqrstvxz]", "");

        }

        public static void Main()

        {

            String str = "GeeeksforGeeks - A Computer Science Portal for Geeks";

            Console.WriteLine(remVowel(str));

        }

    }

    Output:

    eeeoee - A oue iee oa o ee

    Time complexity : O(n) where n is the length of string
    Auxiliary Space: O(1)


    How do you delete a letter from a string Javascript?

    To remove a character from a string, use string replace() and regular expression. This combination is used to remove all occurrences of the particular character, unlike the previous function. A regular expression is used instead of a string along with global property.

    How do I remove a specific part of a string in Javascript?

    To remove all occurrences of a substring from a string, call the replaceAll() method on the string, passing it the substring as the first parameter and an empty string as the second. The replaceAll method will return a new string, where all occurrences of the substring are removed.

    How do you remove all consonants from a string in Java?

    How to remove consonants from a string using regular expressions in Java? Similarly, the following expression matches all the consonants in the given input string. "([^aeiouyAEIOUY0-9\W]+)"; Then you can remove the matched characters by replacing them with the empty string “”, using the replaceAll() method.

    How do you print a vowel in Javascript?

    Printing vowels in the string.
    let s = "welcome".
    let c, i..
    for (i=0; i<=s. length-1; i++){.
    c = s[i] // or s.charAt(i).
    if (c=="a"|| c=="e" || c=="i" || c=="o".
    c=="A"|| c=="E"|| c=="I" || c=="O" || c==.
    log(c).