
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to Accept Strings Containing All Vowels
In order to find that a given string contains all vowels we have to first convert given string into character array so that we can simplify the comparison of each character of given string.
After this put each character into a hash map so that we can check whether our map created from given string contain all vowels or not.We took help of hash map here because there is no concrete method in character array class which could check that it contains all vowels or not.Only way to check is to iterate whole array and compare each character with each vowel which is not appropriate and correct approach,same is with String class so we convert our string into Hash map which would check containing of vowel in one shot.
Now create a array list of character type and contains all vowels.Only step remain is to check whether hash map contains this list in it or not? by using contain all method of Map interface.
In case hash map has contain list of vowels then we can accept the given string otherwise not.
Example
import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; public class StringContainVowels { public static void main(String[] args) { String str1 = "aeiou"; ArrayList<Character> vowelList = new ArrayList<>(Arrays.asList('a','e','i','o','u')); char [] ch1 = str1.toCharArray(); HashMap<Character,Character> hMap = new HashMap(); for(char ch : ch2) { hMap.put(ch,'g'); } if(hMap.keySet().containsAll(vowelList)) { System.out.println("String is accepted" ); } else { System.out.println("String is not accepted"); } } }
Output
String is accepted
Example
import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; public class StringContainVowels { public static void main(String[] args) { String str1 = "aeufgfg"; ArrayList<Character> vowelList = new ArrayList<>(Arrays.asList('a','e','i','o','u')); char [] ch1 = str1.toCharArray(); HashMap<Character,Character> hMap = new HashMap(); for(char ch : ch2) { hMap.put(ch,'g'); } if(hMap.keySet().containsAll(vowelList)) { System.out.println("String is accepted" ); } else { System.out.println("String is not accepted"); } } }
Output
String is not accepted