Question

recursively take two strings that are NOT THE SAME and return the string that consists of...

recursively take two strings that are NOT THE SAME and return the string that consists of all the letters that appear in both string1 and string2 using only string1 and string2 as inputs with NO LOOPS. This must be done recursively.

for example, if string1 is "fourth" and string2 is "faster" the string returned is "frt"

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

Program

class Assignment
{
   //method to return a String contains all the characters
   //that consists of both the String inputs
   public static String stringsConsists(String s1, String s2)
   {
       //base condition
       if(s1.length()==0 || s2.length()==0)
           return "";
       //extract first character
       char c = s1.charAt(0);
       //check if c is found in the String s2
       if(s2.contains(""+c))
           return ("" + c + stringsConsists(s1.substring(1), s2));
       //if c is found in the String s2
       return (stringsConsists(s1.substring(1), s2));
   }
  
   //main method
   public static void main (String[] args)
   {
       String s1 = "fourth";
       String s2 = "faster";
      
       String s3 = stringsConsists(s1, s2);
      
       System.out.println(s3);
   }
}

Output:

frt

N.B. Whether you face any problem then share with me in the comment section, I'll happy to help you.

Add a comment
Know the answer?
Add Answer to:
recursively take two strings that are NOT THE SAME and return the string that consists of...
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
  • recursively take two strings that are NOT THE SAME and return the string that consists of...

    recursively take two strings that are NOT THE SAME and return the string that consists of all the letters that appear in both string1 and string2 using only string1 and string2 as inputs with NO LOOPS. This must be done recursively. for example, if string1 is "fourth" and string2 is "faster" the string returned is "frt"

  • using c and pointers only 1. You will read in two strings from a file cp4in_1.txt...

    using c and pointers only 1. You will read in two strings from a file cp4in_1.txt at a time (there will be 2n strings, and you will read them until EOF) and then do the following. You alternately take characters from the two strings and string them together and create a new string which you will store in a new string variable. You may assume that each string is no more than 20 characters long (not including the null terminator),...

  • In pyhton 3.7 please 2. Write a function that takes, as an argument, either a 12-digit...

    In pyhton 3.7 please 2. Write a function that takes, as an argument, either a 12-digit positive integer n or a string consisting of 12 digits, and calculates the 13th digit (the check digit) for a 13-digit ISBN. Name this function calculateCheckDigit(n). For example, >>>calculateCheckDigit(452678904653) should return 5. As an additional example, >>>calculateCheckDigit(546654945486) should return 8. 3. Write a function that takes, as an argument, an eight-bit binary string and does the following, in this order: • Verifies that it...

  • Write a method will accept an array of Strings. This method should return the string that...

    Write a method will accept an array of Strings. This method should return the string that comes after all the other strings in lexicographical order. (Assume all the Strings only contain lower case letters. In Example: No number, no Capitals and no Symbols) lastString({“hola”, “word”, “night”, “boom”}) → “word” lastString({“java”, “is”, “so”, “much”, “fun”}) → “so” lastString({“i”, “love”, “java”}) → “love” //precondition: words.length >0 public static String lastString(String[] words) { }

  • Write a function addStrings(string1, string2) that takes in two decimals as strings and returns a string...

    Write a function addStrings(string1, string2) that takes in two decimals as strings and returns a string of their sum. *Simply converting strings to numbers and adding them together isn’t acceptable.* The program must be able to handle large decimals. Be sure to touch on these when solving for the solution: Pad the right side Pad the left side Make sure string is same length by putting 0’s where it was missing (be careful w/ decimal points) Make sure to remove...

  • Programming language: Java m: substring length n: input strings d: Hamming distance (mismatch) I: Number of...

    Programming language: Java m: substring length n: input strings d: Hamming distance (mismatch) I: Number of letters in Sample input string (s1, s2, s3) Strings consists of: A, C, G, and T Example outputs: Generate all possible possibilities of length m(4) using the values A, C, G, and T. EX possibilites: {A,A,A,A A,A,A,C.... G,G,G,G} and find all the possible combinations that have the same sequence with a hamming distance of 1 (only 1 difference) Given n input strings of length...

  • JAVA Recursion: For this assignment, you will be working with various methods to manipulate strings using...

    JAVA Recursion: For this assignment, you will be working with various methods to manipulate strings using recursion. The method signatures are included in the starter code below, with a more detailed explanation of what function the method should perform. You will be writing the following methods: stringClean() palindromeChecker() reverseString() totalWord() permutation() You will be using tools in the String class like .substring(), .charAt(), and .length() in all of these methods, so be careful with indices. If you get stuck, think...

  • Prompt the user to enter two character strings. The program will then create a new string...

    Prompt the user to enter two character strings. The program will then create a new string which has the string that appears second, alphabetically, appended to the end of the string that shows up first alphabetically. Use a dash (-') to separate the two strings. You must implement the function string appendStrings(string, string), where the return value is the combined string. Display the newly created string. Your program must be able to handle both uppercase and lowercase characters. You are...

  • Please I need help. Java language Method name: getScores Return value is a String of numbers...

    Please I need help. Java language Method name: getScores Return value is a String of numbers scaled so that the highest number in the parameter String becomes 100 and all the other numbers are moved up by the same amount. This String should also be numbers separated by spaces with no additional characters. The order of the numbers should stay the same, so that a number in the original string has the equivalent scaled number in the returned string in...

  • Write a Python function that will take a string and a list as its only parameters....

    Write a Python function that will take a string and a list as its only parameters. The function needs to return a new string that is the original string with all of the characters appearing in the list parameter replaced with an asterisk(*). The returned string needs to have the remaining characters appear in the same location as the characters they replaced. Hint/Reminder: Note you can make a string out of characters optimally without repeatedly using the concatenation operator.

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
ADVERTISEMENT