Program to parse the Molecules and get the Atoms count

Given a chemical formula as a string, the task is to get the count of atoms in this chemical formula.

Examples:

Input: str = "Fe2H3OH"
Output: Fe 2
        H 4
        O 1

Input: str = "NaCl2NaO2"
Output: Na 2
        Cl 2
        O 2

Approach: The below approach work in Java programming language:

  • Take LinkedHashMap to store atom name(Key) and count(value) in insertion order.
  • Check if the string character is lowercase then add to the previous uppercase string character.
  • Check if the string contains the number then add to the count(value) to their particular atom name(key).
  • Print the atom name(key) as well as count(value).

Below is the implementation of the above approach :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to parse the molecule
// and get the atoms count
  
import java.io.*;
import java.util.*;
  
class GFG {
  
    static void getCount(String str)
    {
  
        // Use LinkedHashmap to store
        // elements in insertion oreder
        Map<String, Integer> mp
            = new LinkedHashMap<String, Integer>();
  
        for (int i = 0; i < str.length(); i++) {
            int count = 0;
  
            // Convert the string element into character
            char c = str.charAt(i);
            String a = " ";
  
            // Convert the character element
            // into string element
            a = String.valueOf(c);
  
            // Check string contains the Capital A - Z value.
            if (a.matches("[A-Z]")) {
  
                for (int j = i + 1; j < str.length(); j++) {
  
                    char d = str.charAt(j);
                    String b = String.valueOf(d);
  
                    // Check string contains the small a-z value.
                    if (b.matches("[a-z]")) {
  
                        a += b;
  
                        if (mp.get(a) == null)
                            mp.put(a, 1);
                        else
                            mp.put(a, mp.get(a) + 1);
                        count = 1;
                    }
  
                    // Check string contains the number value.
                    else if (b.matches("[\\d]")) {
  
                        int k = Integer.parseInt(b);
                        mp.put(a, k);
                        count = 1;
                    }
  
                    else {
                        i = j - 1;
                        break;
                    }
                }
  
                if (count == 0) {
                    if (mp.get(a) == null)
                        mp.put(a, 1);
                    else
                        mp.put(a, mp.get(a) + 1);
                }
            }
        }
  
        System.out.println("\nAtom count:");
        for (Map.Entry<String, Integer>
                 entry : mp.entrySet())
            System.out.println(entry.getKey()
                               + " "
                               + entry.getValue());
  
        mp.clear();
    }
  
    // Driver code
    public static void main(String[] args)
    {
        String str = "Fe2H3OH";
        System.out.println("Given molecule: "
                           + str);
        getCount(str);
    }
}

chevron_right


Output:

Given molecule: Fe2H3OH

Atom count:
Fe 2
H 4
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.