Print number of words, vowels and frequency of each character
Given a string str with uppercase, lowercase and special characters. The input string is to end with either a space or a dot. The problem is to calculate the number of words, vowels and frequency of each character of the string in a separate line.
Example :
Input : How Good GOD Is. Output : Number of words = 4 Number of vowels = 5 Number of upper case characters = 6 Character = Frequency = 3 Character = . Frequency = 1 Character = D Frequency = 1 Character = G Frequency = 2 Character = H Frequency = 1 Character = I Frequency = 1 Character = O Frequency = 1 Character = d Frequency = 1 Character = o Frequency = 3 Character = s Frequency = 1 Character = w Frequency = 1
Approach : We use a TreeMap to store characters and their frequencies. TreeMap is used to get the output in sorted order.
Below is Java implementation of above approach :
// Java program to print Number of Words, // Vowels and Frequency of Each Character import java.util.*; import java.lang.*; import java.io.*; public class Stringfun { String str = "Geeks for Geeks."; void words() { int wCount = 0, uCount = 0, vCount = 0; for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); switch (c) { case ' ': case '.': wCount++; // more delimiters can be given } switch (c) { // program for calculating number of vowels case 'A': case 'E': case 'I': case 'O': case 'U': case 'a': case 'e': case 'i': case 'o': case 'u': vCount++; } if (c >= 65 && c <= 90) { uCount++; } } System.out.println("Number of words = " + wCount); System.out.println("Number of vowels = " + vCount); System.out.println("Number of upper case characters = " + uCount); } // Function to calculate the frequency // of each character in the string void frequency() { // Creates an empty TreeMap TreeMap<Character, Integer> hmap = new TreeMap<Character, Integer>(); // Traverse through the given array for (int i = 0; i < str.length(); i++) { Integer c = hmap.get(str.charAt(i)); // If this is first occurrence of element if (hmap.get(str.charAt(i)) == null) hmap.put(str.charAt(i), 1); // If elements already exists in hash map else hmap.put(str.charAt(i), ++c); } // Print result for (Map.Entry m:hmap.entrySet()) System.out.println("Character = " + m.getKey() + " Frequency = " + m.getValue()); } // Driver program to run and test above program public static void main(String args[]) throws IOException { Stringfun obj = new Stringfun(); obj.words(); obj.frequency(); } } |
Output :
Number of words = 3 Number of vowels = 5 Number of upper case characters = 2 Character = Frequency = 2 Character = . Frequency = 1 Character = G Frequency = 2 Character = e Frequency = 4 Character = f Frequency = 1 Character = k Frequency = 2 Character = o Frequency = 1 Character = r Frequency = 1 Character = s Frequency = 2
Time Complexity : O(n), where n is the number of characters in the string.
Auxiliary Space : O(1).
Recommended Posts:
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Find the count of M character words which have at least one character repeated
- Queries to print the character that occurs the maximum number of times in a given range
- Longest sub-string having frequency of each character less than equal to k
- Character whose frequency is equal to the sum of frequencies of other characters of the given string
- Check if the frequency of any character is more than half the length of the string
- Check if a two character string can be made using given words
- Print an N x M matrix such that each row and column has all the vowels in it
- Print reverse string after removing vowels
- Print all possible words from phone digits
- Given a sequence of words, print all anagrams together using STL
- Print all unique words of a String
- Given a sequence of words, print all anagrams together | Set 1
- Given a sequence of words, print all anagrams together | Set 2
- Print all funny words in a string
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.



