1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
OlgaM077 [116]
2 years ago
13

Write the WordSandwich method allSandwiches. This method creates and returns a new array of String objects as follows. Each elem

ent of the array will be a sandwich of str1 and str2: that is the characters of str2 are inserted into str1. The first String in the array consists of 1 letter of str1 followed by str2, followed by the rest of the letters of str1. For each subsequent String in the array the position in str1 where str2 starts is shifted by 1 until the final string in the array which consists of all but 1 letter of str1 followed by str2, followed by the final remaining letter of str1. For example, the call allSandwiches("bread", "ham") will return the following array.
Computers and Technology
1 answer:
nalin [4]2 years ago
5 0

Answer:

import java.util.*;

class Main

{

private String[] totalSandwiches(String string1,String string2)

{

int totalElements = string1.length()-1;

String ans[] = new String[totalElements];

for(int i=0;i<totalElements;i++)

{

String temp = "";

for(int j=0;j<=i;j++)

{

temp+=string1.charAt(j);

}

temp+=string2;

for(int j=i+1;j<string1.length();j++)

{

temp+=string1.charAt(j);

}

ans[i] = temp;

}

return ans;

}

public static void main(String[] args)

{

Scanner sc = new Scanner(System.in);

Main obj = new Main();

String string1,string2;

System.out.print("Enter the 1st String: ");

string1 = sc.nextLine();

while(string1.length()<2)

{

System.out.print("The size of string 1 should be atleast 2 to make a sandwich. Enter the string again: ");

string1 = sc.nextLine();

}

System.out.print("Enter the 2nd String: ");

string2 = sc.nextLine();

while(string2.length()==0)

{

System.out.print("The size of string 2 should be atleast 1 to make a sandwich. Enter the string again: ");

string2 = sc.nextLine();

}

System.out.println("Following are the sandwiches: ");

String ans[] = obj.totalSandwiches(string1,string2);

for(int i=0;i<ans.length;i++)

{

System.out.println(ans[i]);

}

}

}

Explanation:

  • Run a while loop upto the total no. of elements and calculate number of elements from the string1 that should come before string2 .
  • Append the remaining characters of string1 are to temp.
  • The ans array stores the temp at ith index.
  • string1 characters are stored by the variable j.
  • Sandwich is not possible if string1 has less than 2 elements and possible if string2 has no elements.
  • Finally display the sandwich strings.
You might be interested in
The following are part of characteristics of a software requirement specification.
marusya05 [52]

Answer:

should there be 'except' in the question?

6 0
3 years ago
4. Select the correct answer.
3241004551 [841]

Answer: B.  PDF

Explanation:

Portable Document Format (PDF) is such an important document that it is considered the international standard for information and document exchange. It is used for letters, resumes and even electronic contracts.

PDF was developed by Adobe to make it independent of the device or application it is being viewed on and combines layers of information into one layer thereby making it a flat document.

7 0
3 years ago
Which of the following is NOT necessary for organizing data to make it easier to sort?
Elenna [48]

Answer:

All the data must be the same font and font size is not necessary for data sorting.

Explanation:

The most easier and frequently used tool for data organizing and sorting is Microsoft's excel or google spreadsheet. Sorting deals with arrangement of  data values in a particular sequence or order according to defined rules. For example data can be sort in ascending or descending order as per values or names in list.

7 0
3 years ago
Please NEED HELP ASAP WILL MARK BRAINLIEST ONLY #8
ValentinkaMS [17]

Answer:

I think its A

Explanation:

6 0
2 years ago
What is an example of fibre optic cable<br>​
Arte-miy333 [17]

Answer:

printing cable

Explanation:

is a cable used to transfer information from a computer to the printer in packages

5 0
2 years ago
Other questions:
  • A ________ -tier design includes a middle layer between the client and server that processes the client requests and translates
    11·1 answer
  • Which symbol is used to separate a worksheet name from a cell reference?
    10·1 answer
  • There are ways to perform computer commands quickly and multiple times. <br> a. True <br> b. False
    11·2 answers
  • WHEN COPYING EXISTING SPREADSHEET DATA TO A WORD DOCUMENT YOU NEED TO SELECT?
    12·1 answer
  • Which of the following is a default letter assigned for the primary hard drive
    6·2 answers
  • Write a function to output an array of ints on a single line. Funtion Should take an array and an array length and return a void
    9·1 answer
  • Write a do-while loop that continues to prompt a user to enter a number less than 100, until the entered number is actually less
    6·2 answers
  • Citing Wikipedia as a reference is not considered a good practice because 1.leverages a neutral point of view. 2.hosts unopinion
    8·1 answer
  • Which type of relationship is responsible for teaching you values and how to communicate?
    9·1 answer
  • What dictionary operation can you use to remove all the keys within the dictionary-named contacts?
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!