Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write an application that allows a user to enter two Strings. Output the number

ID: 3797943 • Letter: W

Question

Write an application that allows a user to enter two Strings. Output the number of characters in the first String that also appear in the second String, and output those characters. The figure below shows two typical executions. Save the file as CountMatches.java.

Command Prompt GE Java Java CountMatches Enter a string open Enter another string close 2 characters in open are also found in close The characters in open that are also in close are as follows C: Jav a java CountMatches Entcr a string puppy Enter another string pancake 3 characters in puppy are also found in pancake The characters in puppy that are also in pancake are as follows p p p C: Java Figure 7-20 Two typical executions of the CountMatches application

Explanation / Answer

program:-

import java.util.Scanner;
public class Countcharinstring
{
    public static void main(String[] args)
    {
        char c[]=new char[10];
        Scanner scanner = new Scanner(System.in);
        String string1,string2;
        System.out.println("Enter First String:");
        string1=(scanner.next());
        System.out.println("Enter Second String:");
        string2=(scanner.next());
        int count=0;
        int m=-1,t;
      
        for(int i=0;i<string1.length();i++)
        {
            t = 1;
            for(int k=0;k<=m;k++)
            {
                if(string1.charAt(i)==c[k])
                {
                    t=0;
                }
            }
            if(t==1)
            {
                for(int j=0;j<string2.length();j++)
                {
                    if(string1.charAt(i)==string2.charAt(j))
                    {
                        count++;
                        m++;
                        c[m]=string1.charAt(i);
                        break;
                    }
                }
            }
        }
        System.out.println("Number of matched cheractors is: "+count);
        System.out.println("The cheractors are ");
        for(int l=0;l<=m;l++)
        {
            System.out.print(c[l]+" ");
        }
        System.out.println();
    }
}

output:-

run:
Enter First String:
open
Enter Second String:
close
Number of matched cheractors is: 2
The cheractors are
o e
BUILD SUCCESSFUL (total time: 28 seconds)


run:
Enter First String:
pphaniii
Enter Second String:
phani
Number of matched cheractors is: 5
The cheractors are
p h a n i
BUILD SUCCESSFUL (total time: 10 seconds)

run:
Enter First String:
phani
Enter Second String:
pphaniii
Number of matched cheractors is: 5
The cheractors are
p h a n i
BUILD SUCCESSFUL (total time: 12 seconds)

run:
Enter First String:
pppha
Enter Second String:
happp
Number of matched cheractors is: 3
The cheractors are
p h a
BUILD SUCCESSFUL (total time: 15 seconds)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote