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 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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();
    }
}

chevron_right


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).



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.