Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

Frequently Asked Java Programs 31 – Java Program to Find Common Characters in Two Given Strings

Posted on 04/23/2025 By admin

Hello Folks,

As part of Frequently Asked Java Programs In Interviews For Freshers And Experienced, in this post we will see a java program Find Common Characters in Two Given Strings.

String s1= “ABCDEF”

String s2= “DEFXYZ”

Output: DEF

This problem can be solved with or without using collection concepts in Java. I will explain both solutions here because in interview, you could be asked in any ways.

  1. We need to iterate char by char a given string with smaller length.
  2. Take the first char of a string say S1 and check if String say S2 contains that char. If yes, store in another resultant String. Repeat the same process for each char of S1.
  3. After completion of iteration, resultant string will have common characters in both Strings.

package StringPrograms;

import java.util.Scanner;

public class CommonCharInTwoStrings {

        public static String commonChars(String str1, String str2) {
                StringBuilder commonChars = new StringBuilder();

                if (str1.length() > 0 & str2.length() > 0) {
                        // We should iterate the smallest string in length.
                        String toBeIterated = (str1.length() > str2.length() ? str2 : str1);
                        // Once string to be iterated is finalized, get string to be checked
                        String toBeChecked = toBeIterated.equals(str1) ? str2 : str1;
                        System.out.println("String to be iterated: " + toBeIterated);
                        System.out.println("String to be checked: " + toBeChecked);

                        // Iterating a string char by char
                        for (int i = 0; i < toBeIterated.length(); i++) {
                                // Check for common char
                                if (toBeChecked.contains(Character.toString(toBeIterated.charAt(i)))) {
                                        // If contains append it to resultant string
                                        commonChars.append(Character.toString(toBeIterated.charAt(i)));
                                }
                        }
                        return commonChars.toString();
                } else
                        return "";
        }

        public static void main(String[] args) {

                // User input for the string to know length
                Scanner sc = new Scanner(System.in);
                System.out.println("Please enter the first string");

                String string1 = sc.nextLine();

                System.out.println("Please enter the second string");

                String string2 = sc.nextLine();

                sc.close();

                System.out.println("Output is :" + commonChars(string1, string2));
        }

}

Output:-

Please enter the first string ABC Please enter the second string ABCDEF String to be iterated: ABC String to be checked: ABCDEF Output is :ABC======================================================Please enter the first string selenium Please enter the second string winium String to be iterated: winium String to be checked: selenium

Output is :inium

Uncategorized

Post navigation

Previous Post: Upload multiple files in Selenium
Next Post: explicit wait in selenium

Related Posts

February 26, 2018 – Make Selenium Easy Uncategorized
March 31, 2017 – Make Selenium Easy Uncategorized
URL Loading1503830083282 – Make Selenium Easy Uncategorized
January 25, 2019 – Make Selenium Easy Uncategorized
yatraMultiCLass – Make Selenium Easy Uncategorized
JavaScript Way To Handle Calendar In Selenium | Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark