#2 Finding a Common Character in Three Strings using Java (Without Using a Method)

#2 Finding a Common Character in Three Strings using Java (Without Using a Method)

Problem Statement:

Given three input strings, our task is to identify a character that is present in all three strings. If such a character exists, we'll return it. If no common character is found, we'll return -1.

Example:

Input:

String 1: "123"

String 2: "345"

String 3: "563"

Output: '3'

public class CommonCharacterFinderWithoutMethod {

    public static void main(String[] args) {
        String inputStr1 = "123";
        String inputStr2 = "345";
        String inputStr3 = "563";
        boolean commonFound = false;

        for (char c : inputStr1.toCharArray()) {
            boolean foundInStr2 = false;
            boolean foundInStr3 = false;

            for (char c2 : inputStr2.toCharArray()) {
                if (c == c2) {
                    foundInStr2 = true;
                    break;
                }
            }

            for (char c3 : inputStr3.toCharArray()) {
                if (c == c3) {
                    foundInStr3 = true;
                    break;
                }
            }

            if (foundInStr2 && foundInStr3) {
                System.out.println("Common character found: " + c);
                commonFound = true;
                break;
            }
        }

        if (!commonFound) {
            System.out.println("No common character found.");
        }
    }
}        

Explanation:

In this solution, we've opted for a direct approach without utilizing a separate method. The key element is the nested loop structure that allows us to compare characters across the three input strings—inputStr1, inputStr2, and inputStr3. Let's break down the explanation into steps:

  1. Initialization: We start by initializing the necessary variables. We have inputStr1, inputStr2, and inputStr3 to represent the three input strings. We also have a commonFound boolean flag that indicates whether a common character has been found.
  2. Outer Loop (First String): We iterate through each character of the first input string, inputStr1. For each character (c), we set up two boolean flags, foundInStr2 and foundInStr3, to track whether the character is present in the second and third input strings.
  3. Inner Loops (Second and Third Strings): Within the loop for inputStr1, we have two nested loops—one for inputStr2 and another for inputStr3. These loops compare each character of inputStr1 with characters in inputStr2 and inputStr3, respectively. If a match is found in either of these strings, the corresponding flag is set to true, and the loop breaks.
  4. Common Character Check: After the inner loops, we check if both foundInStr2 and foundInStr3 are true. If they are, it means the character c is present in all three strings. In that case, we print the message indicating the common character, set commonFound to true, and exit the outer loop using break.
  5. Display Result: If no common character is found after iterating through all characters of inputStr1, we display the "No common character found" message.

By using this direct approach, we demonstrate how to efficiently find a common character among three input strings in Java without the use of a inbuilt method.

To view or add a comment, sign in

More articles by RUPA SHREE K

Others also viewed

Explore content categories