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 :
// 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
filter_none
Output:
Given molecule: Fe2H3OH Atom count: Fe 2 H 4 O 1
Recommended Posts:
- Program to Parse a comma separated string in C++
- Program to Count numbers on fingers
- Program to count occurrence of a given character in a string
- Program to count digits in an integer (4 Different Methods)
- Python program to count Even and Odd numbers in a List
- Java program to count the characters in each word in a given sentence
- Program to count vowels in a string (Iterative and Recursive)
- Program to find the count of coins of each type from the given ratio
- Python program to count number of vowels using sets in given string
- Program to count number of distinct Squares and Cubes upto N
- Python program to count positive and negative numbers in a list
- Program to count vowels, consonant, digits and special characters in string.
- Longest subarray having count of 1s one more than count of 0s
- Count pairs with given sum
- Count numbers that don't contain 3
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.



