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]
3 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]3 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
When a primary key combines two or more fields, it is called a ____ key?
Tanya [424]
<span>When a primary key combines two or more fields, it is called a ____ key
the answer is Constructed key</span>
6 0
3 years ago
Which structural semantic will this HTML code snippet form?
Yakvenalex [24]

Answer:

D. Block

Explanation:

Semantic HTML or semantic markup is HTML that introduces meaning to the web page rather than just presentation. For example, a <p> tag indicates that the enclosed text is a paragraph. This is both semantic and presentational because people know what paragraphs are, and browsers know how to display them.

3 0
3 years ago
Suppose you are choosing between the following three algorithms:
Pavel [41]

Answer:

Algorithm C is chosen

Explanation:

<u>For Algorithm A </u>

T(n) = 5 * T ( n/2 ) + 0(n)

where : a = 5 , b = 2 , ∝ = 1

attached below is the remaining part of the solution

6 0
3 years ago
Which are examples of primary sources? Check all that apply.
Marrrta [24]

Answer:

A, D, E

Explanation:

Diaries, photographs, and speeches are all forms of primary sources, whereas biographies and newspaper articles are written by a secondary source.

5 0
3 years ago
Read 2 more answers
Where does append add the new elements?
ZanzabumX [31]

Answer:

"Option 1: To the end of an array." is the correct answer.

Explanation:

The word "Append" means adding to the end of a document.

Appending means whatever new content is added, it is added at the end of the document or data structure.

Similarly,

Appending an array means adding new elements to the end of the array.

Hence,

"Option 1: To the end of an array." is the correct answer.

7 0
3 years ago
Other questions:
  • Samantha was calculating a mathematical formula on an electronic spreadsheet. She used multiple values to recalculate the formul
    7·2 answers
  • In Shoreville, the last low tide was at 12:00 a.m. About what time will the next high tide occur?
    8·2 answers
  • While waiting to be seated at a restaurant, Jason receives a customer loyalty coupon through an app on his mobile phone for half
    7·1 answer
  • Simple geometry can compute the height of an object from the the object's shadow length and shadow angle using the formula: tan(
    12·1 answer
  • What is true regarding the cellular phone concept? a. a single radio broadcast tower system enables greater frequency reuse comp
    15·1 answer
  • To help determine to which application a transmission should be delivered on a particular computer, TCP uses the application lay
    8·2 answers
  • Date Class Constructor – assigns fields to appropriate formal parameter – using the setters so error checking will occur. The co
    6·1 answer
  • Write Java code that creates an array of n integers myArray, by taking n integers from the user with duplications, and do the fo
    8·1 answer
  • Which document outlines the activities carried out during testing?
    9·1 answer
  • Write a Python statement that displays the value referenced by the number variable formatted as1,234,567.5
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!