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"

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

Please let me know if anything is required. I have completed this code in python. Please let me know if code is needed in any other language. I am happy to help you.Please follow the indentation as shown in the screenshot.

Added the code in java also

Code in java :

import java.io.*;

public class Main
{
   public static void main(String[] args) throws IOException{
      
      
       BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      
       System.out.println("Enter the first string : "); //taking string 1 input from the user
       String str1 = br.readLine();
       System.out.println("Enter the first string : "); //taking string 2 input from the user
       String str2 = br.readLine();
  
String p="";
      
       Main obj = new Main();
       String k = obj.recursive_same_letters(str1,str2,0,p); //calling the function
System.out.println("The string that consists of all the letters that appear in both string1 and string2 is : "+k); //printing the result
      
      
      
   }
  
   String recursive_same_letters(String s1,String s2,int i, String p)
   {
     
   if(i<s1.length()) //if i is less than the length of the string 1 then it continue to check for the characters
{ if(s2.indexOf(s1.charAt(i) )>= 0 ) // if ith character of s1 in s2 then adding it into a string
{
p=p+s1.charAt(i);
}
i=i+1; //increasing the index
return recursive_same_letters(s1,s2,i,p); //calling the function recursively
}
else //condition if checking of all characters done
{
return p; //returning the string that consists of all the characters that appear in both s1tring and string2
}
  
   }
}

Sample output:

input Enter the first string : fourth Enter the first string : faster The string that consists of all the letters that appear

Code screenshot :

2 #recursive function find the string that consists of all the letters that appear in both string1 and string2 3- def recursi

Sample output1:

Input Enter the first string : fourth Enter the second string : first, The string that consists of all the letters that appea

Sample output2:

nipul Enter the first string : rizwan Enter the second string : rayyan The string that consists of all the letters that appea

Copyable code:


#recursive function find the string that consists of all the letters that appear in both string1 and string2
def recursive_same_letters(s1,s2,i,p):
if(i<len(s1)): #if i is less than the length of the string 1 then it continue to check for the characters
if(s1[i] in s2): # if ith character of s1 in s2 then adding it into a string
p=p+s1[i]
i=i+1 #increasing the index
return recursive_same_letters(s1,s2,i,p) #calling the function recursively
else: #condition if checking of all characters done
return p #returning the string that consists of all the characters that appear in both s1tring and string2


str1=input("Enter the first string : ") #taking string 1 input from the user
str2=input("Enter the second string : ")#taking string 2 input from the user
p=""
k=recursive_same_letters(str1,str2,0,p) #calling the function
print("The string that consists of all the letters that appear in both string1 and string2 is : ",k) #printing the result

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" LANGUAGE IS JAVA

  • 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...

  • 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...

  • 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...

  • PYTHON The function longest that returns the longest of two strings. The function count_over that takes...

    PYTHON The function longest that returns the longest of two strings. The function count_over that takes a list of numbers and an integer nand returns the count of the numbers that are over n. The function bmi that takes two parameters height and weight and returns the value of the body mass index: a person's weight in kg divided by the square of their height in meters. def longest(string1, string2): """Return the longest string from the two arguments, if the...

  • 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) { }

  • Given a string, recursively compress all sets of repeating adjacent chars within an existing string to...

    Given a string, recursively compress all sets of repeating adjacent chars within an existing string to a single char. For example, "XVxzzz" yields "xyz" <pre> ceoveReaeatsl"fffaaac Quuutreturns "far Out" eoveReneatsC"nogoge wogorcriiies") returns "no worries" remaveReneats.C" Tomorrow") returns "Iomeo" s /pre Qparam stc a string of characters @return a version of the original string with all repeating adjacent sequences of the same character, reduced to a single character public static String removeRepeats (String str) { ou are forbidden to use any...

  • /**    Given a String and an array of two Strings,    return a three String...

    /**    Given a String and an array of two Strings,    return a three String array containing the strings in alphabetical order.    Note: Capital letters count    sort3Strings("wallace", {"washington", "irving"}) -> {"irving", "wallace", "washington"}    sort3Strings("wallace", {"washington", "Irving"}) -> {"Irving", "wallace", "washington"}    sort3Strings("Washington", {"irving", wallace"}) -> {"Washington", "irving", "wallace"}    sort3Strings("washington", {"washington", "Washington"}) -> {"Washington", "washington", "washington"} **/ public static String[] sort3Strings(String stringValue, String[] stringArray) {    //your code here    return new String[1]; }//end sort3Strings   ...

  • Write a function named words_in_both that takes two strings as parameters and returns a set of...

    Write a function named words_in_both that takes two strings as parameters and returns a set of the words contained in both strings. You can assume all characters are letters or spaces. Capitalization shouldn't matter: "to", "To", "tO", and "TO" should all count as the same word. The words in the set should be all lower-case. For example, if one string contains "To", and the other string contains "TO", then the set should contain "to". The file must be named: words_in_both.py...

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