Frequently Asked Java Program 20: Java Program to Find Missing Alphabets in Given String
This program was asked in Siemens.
Problem Statement:
You need to find missing alphabets from a given string. E.g.
User input: “Amod Mahajan”
Output: [B, C, D, E, F, G, H, I, J, K, L, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, b, c, e, f, g, i, k, l, p, q, r, s, t, u, v, w, x, y, z]
Solution:
Logic step by step:
Step 1: Remove all whitespaces from given string as we just need characters of given string. To do this, we can use replaceAll() method of String class. I will remove all white spaces into empty space. E.g. userInput.replaceAll(” “, “”)
Step 2: We need to extract each character from given string and store in an array. We can do this using split() method. E.g.
String [] inputStringArray = userInput.split(“”);
Step 3: Create an array of alphabets including upper and lower alphabets.
String alphabets[]= “abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ”.split(“”);
Step 4: Now create a Set of above arrays. Set is useful to remove duplicate characters and find intersection of arrays.
Complete Java Program:
[java]
package StringPrograms;
import java.util.Arrays; import java.util.HashSet;
import java.util.Scanner;