Question

String s1 is said to overlap String s2 if all of the characters in s1 also...

String s1 is said to overlap String s2 if all of the characters in s1 also appear in s2. Write a set of code that will set the boolean variable overlap to true if s1 overlaps s2 and false otherwise. Assume both s1 and s2 have already been input.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

public class Main
{
   public static void main(String[] args) {
       String s1="Hello World";
       String s2="oWl";
       boolean overlap=true;
       for(int i=0;i<s2.length();i++)
       {
       char ch=s2.charAt(i);//take character
       int f=0;
       for(int j=0;j<s1.length();j++)
       {
       //search if it is present in s1
       if(ch==s1.charAt(j))
       {
       f=1;//if it is present search for next
       break;
       }
       }
       if(f==0)//if not found make it false
       {
       overlap=false;
       break;
       }
      
       }
       if(overlap)
       {
       System.out.println("overlaps");
       }
       else
       {
       System.out.println("Does Not overlaps");
       }
   }
}

overlaps

Add a comment
Know the answer?
Add Answer to:
String s1 is said to overlap String s2 if all of the characters in s1 also...
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
  • Write a recursive method isReverse(String s1, String s2) that takes two strings and returns true if...

    Write a recursive method isReverse(String s1, String s2) that takes two strings and returns true if s1 is the reverse of s2, false otherwise. Then, draw the sequence of recursive calls for the following cases. Submit your diagrams in a PDF file called isReverseTrace.pdf. isReverse("happy", "yppah") will return true isReverse("cool", "loac") will return false isReverse("", "") will return true

  • Write the function in python and show the code 2. Write a function jscore (s1, s2)...

    Write the function in python and show the code 2. Write a function jscore (s1, s2) that takes two strings s1 and s2 as inputs and returns the Jotto score of s1 compared with s2 -.e., the number of characters in s1 that are shared by s2. The positions and the order of the shared characters within each string do not matter. Repeated letters are counted multiple times, as long as they appear multiple times in both strings. For example...

  • Write a Java program for all items in Question Use System.out.println() for each resulting string. Let...

    Write a Java program for all items in Question Use System.out.println() for each resulting string. Let s1 be " Welcome " and s2 be " welcome ". Write the code for the following statements: a. Check whether s1 is equal to s2 and assign the result to a Boolean variable isEqual. b. Check whether s1 is equal to s2, ignoring case, and assign the result to a Boolean variable isEqual. c. Compare s1 with s2 and assign the result to...

  • Java A bigram is a pair of adjacent words in a sequence. Bigrams overlap so that...

    Java A bigram is a pair of adjacent words in a sequence. Bigrams overlap so that in the sequence "a b. c d", the bigrams are ("a", "b."), ("b.", "c"), ("c", "d"). You will write a simple parser which builds a bigram model based on input text and will allow checking sentences and generating sequences. To do so, you should take advantage of Java’s collection classes including Maps. Create a class called Bigram. The class will have a constructor which...

  • Question 8 A C-string is a sequence of characters terminated by the null character. True False...

    Question 8 A C-string is a sequence of characters terminated by the null character. True False D Question 9 What is the longest C-string that can be put into this C-string variable? char s[9]; There is not enough information to determine the 8th Question 10 D AC string variable is just an array of characters without the null character. True False Question 11 5 pts If you have already removed a character in char variable ch) from the input stream,...

  • 1. Write a function called ordinal_sum that accepts a string argument and returns the sum of...

    1. Write a function called ordinal_sum that accepts a string argument and returns the sum of the ordinal values of each character in the string. The ordinal value of a character is its numeric Unicode code point in decimal. 2. Write code that creates a Python set containing each unique character in a string named my_string. For example, if the string is 'hello', the set would be {'h', 'e', 'l', 'o'} (in any order). Assign the set to a variable...

  • Write a C program that takes two sets of characters entered by the user and merge...

    Write a C program that takes two sets of characters entered by the user and merge them character by character. Enter the first set of characters: dfn h ate Enter the second set of characters: eedtecsl Output: defend the castle Your program should include the following function: void merge(char *s3, char *s1, char *s2); The function expects s3 to point to a string containing a string that combines s1 and s2 letter by letter. The first set might be longer...

  • Add the package hw5p3 to your FirstName-LastName-HW5 project. public static void main(String[] args) { decideAnagrams("Mother In...

    Add the package hw5p3 to your FirstName-LastName-HW5 project. public static void main(String[] args) { decideAnagrams("Mother In Law", "Hitler Woman"); decideAnagrams("keEp", "peeK"); decideAnagrams("SiLeNt CAT", "LisTen AcT"); decideAnagrams("Debit Card", "Bad Credit"); decideAnagrams("School MASTER", "The ClassROOM"); decideAnagrams("DORMITORY", "Dirty Room"); decideAnagrams("ASTRONOMERS", "NO MORE STARS"); decideAnagrams("Toss", "Shot"); decideAnagrams("joy", "enjoy"); } static void decideAnagrams(String s1, String s2) { // Removing all white spaces from s1 and s2 String copyOfs1 = s1.replaceAll("\\s", ""); String copyOfs2 = s2.replaceAll("\\s", ""); // Initially setting status as true boolean areAnagrams =...

  • IN C++ PLEASE Write a class Food that has a string attribute for name and a...

    IN C++ PLEASE Write a class Food that has a string attribute for name and a Boolean value for hasBeenEaten and a Boolean value for isSpoiled, default both Booleans to false add a method spoil() which sets isSpoiled to true Add a method eat() If hasBeenEaten is false, if isSpoiled is false, return a string that says "you eat the <insert name here>, yum!", then set hasBeenEaten to true if isSpoiled is true, return a string that says "I wouldn't...

  • Write a class StringsAndThings. The class has one instance variable of type String and a constructor...

    Write a class StringsAndThings. The class has one instance variable of type String and a constructor with a parameter to initialize the instance variable. The parameter could contain any characters, including letters, digits, spaces, special characters (+, -, $, @,...), and punctuation marks. Write methods: countNonLetters - that will count how many of the characters in the string are not letters. You can use Character's isLetter method. moreVowels - which returns true if the String parameter has more vowels than...

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