Question

(JAVA question) Write a method named intersection that accepts two strings as the arguments, returns the...

(JAVA question)

Write a method named intersection that accepts two strings as the arguments, returns the characters that occur in both strings. For example, if the arguments are “apple#123” and “banana$#345”, your program should return “a#3”.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class StringsIntersection {

    public static String intersection(String s1, String s2) {
        String common = "";
        for (int i = 0; i < s1.length(); i++) {
            if (s2.indexOf(s1.charAt(i)) >= 0) {
                common += s1.charAt(i);
            }
        }
        return common;
    }

    public static void main(String[] args) {
        System.out.println(intersection("apple#123", "banana$#345"));
    }
}

Add a comment
Know the answer?
Add Answer to:
(JAVA question) Write a method named intersection that accepts two strings as the arguments, returns the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT