Question

In: Computer Science

public static java.lang.String mergeWithRuns​(java.lang.String t, java.lang.String s) Merges two strings together, using alternating characters from each,...

public static java.lang.String mergeWithRuns​(java.lang.String t, java.lang.String s)

Merges two strings together, using alternating characters from each, except that runs of the same character are kept together. For example,

  • mergeWithRuns("abcde", "xyz") returns "axbyczde"
  • mergeWithRuns("abbbbcde", "xyzzz") returns "axbbbbyczzzde"

Either or both of the strings may be empty. If the first string is nonempty, its first character will be first in the returned string.

Parameters:

t - first string

s - second string

Returns:

string obtained by merging characters from t and s, preserving runs

Solutions

Expert Solution

Explanation:

import java.io.*;

class Merging // Creating Main Class

{

// Creating and Implementing of method

public static java.lang.String mergePreservingRuns(java.lang.String t,java.lang.String s)

{

java.lang.String str=""; // Declare String variable str

int i=0,j=0; // Declare and Initialize two integer variables i and j

while(i<t.length() && j<s.length()) // Create while loop for two strings have characters left

{

char pChar=t.charAt(i++); // assign character by character to pChar

str+=pChar; // store into str

while(i<t.length() && t.charAt(i)==pChar) // This while loop for check equal characters until length of first string

{

str+=pChar; // then store character by character into str

i++;

}

// simillarly, second string

pChar=s.charAt(j++);

str+=pChar;

while(j<s.length()&&s.charAt(j)==pChar)

{

str+=pChar;

j++;

}

}

// the final string will be stored into the str of two string

while(i<t.length())

str+=t.charAt(i++);

while(j<s.length())

str+=t.charAt(j++);



return str; // this is resultant string to return

}

public static void main(String args[])

{

String s1,s2;

// call mergePreservingRuns() method

s1=mergePreservingRuns("abcde","xyz");

s2=mergePreservingRuns("abbbbcde", "xyzzz");

// print resultant strings

System.out.println("Merging two String alternative Characters: "+s1);

System.out.println("Merging two String alternative Characters: "+s2);

}

}

OUTPUT

F:\>javac Merging.java

F:\>java Merging

Merging two String alternative Characters: axbyczde

Merging two String alternative Characters: axbbbbyczzzde

...... Please like if it's helpfull for you and comment if any query...........


Related Solutions

You are required to use C++ static or dynamic arrays of characters to store c-strings. You...
You are required to use C++ static or dynamic arrays of characters to store c-strings. You are NOT allowed to use any C++ string data type variable for any purpose. Moreover, you are allowed to add any include directive. You are not allowed to include string, cstdlib or math libraries. Also, you are not allowed to use any built-in functions of c-strings. can someone help with the third, fourth, and fifth functions? I tried many ways but i cannot figure...
You are required to use C++ static or dynamic arrays of characters to store c-strings. You...
You are required to use C++ static or dynamic arrays of characters to store c-strings. You are NOT allowed to use any C++ string data type variable for any purpose. Moreover, you are allowed to add any include directive. You are not allowed to include string, cstdlib or math libraries. Also, you are not allowed to use any built-in functions of c-strings. can someone help with the third, fourth, and fifth functions? I tried many ways but i cannot figure...
Using this BubbleSort implementation in Java: public class BubbleSort<T extends Comparable<T>> {    private static <T...
Using this BubbleSort implementation in Java: public class BubbleSort<T extends Comparable<T>> {    private static <T extends Comparable<T>>    void swap(T[] data, int index1, int index2)    {       T temp = data[index1];       data[index1] = data[index2];       data[index2] = temp;    }    public void sort(T[] data)    {       int position, scan;       for (position = data.length - 1; position >= 0; position--)       {          for (scan = 0; scan <= position - 1; scan++)          {...
public static int countSubstrings​(java.lang.String t, java.lang.String s, boolean allowOverlap) Counts the number of times that one...
public static int countSubstrings​(java.lang.String t, java.lang.String s, boolean allowOverlap) Counts the number of times that one string occurs as a substring in another, optionally allowing the occurrences to overlap. For example: countSubstrings("aa", "aaaaa", false) returns 2 countSubstrings("aa", "aaaaa", true) returns 4 countSubstrings("aa", "ababab", true) returns 0 Parameters: t - string we are looking for ("target") s - string in which we are looking ("source") allowOverlap - true if occurrences of t are allowed to overlap Returns: number of times t...
2. Non-Local Strings For this question, a block is a sequence of 20 characters, where each...
2. Non-Local Strings For this question, a block is a sequence of 20 characters, where each character is one of the 26 lowercase letters a-z. For example, these are blocks: iwpiybhunrplsovrowyt rpulxfsqrixjhrtjmcrr fxfpwdhwgxtdaqtmxmlf How many different blocks are there? A block is squarefree if no character appears two times consecutively. The first and third example above are squarefree, but the second example is not because of the two consecutive occurrences of r. How many squarefree blocks are there? A block...
CODE: C# using System; public static class Lab6 { public static void Main() { // declare...
CODE: C# using System; public static class Lab6 { public static void Main() { // declare variables int hrsWrked; double ratePay, taxRate, grossPay, netPay=0; string lastName; // enter the employee's last name Console.Write("Enter the last name of the employee => "); lastName = Console.ReadLine(); // enter (and validate) the number of hours worked (positive number) do { Console.Write("Enter the number of hours worked (> 0) => "); hrsWrked = Convert.ToInt32(Console.ReadLine()); } while (hrsWrked < 0); // enter (and validate) the...
Write a static method startsWith that inputs two Strings and returns a boolean. If the first...
Write a static method startsWith that inputs two Strings and returns a boolean. If the first input starts with the substring that is the second input, then the method returns true; otherwise, it returns false. For example, startsWith( "radar installation", "rad" ) returns true startsWith( "radar installation", "installation" ) returns false startsWith( "radar installation", "" ) returns true startsWith( "", "a" ) returns false startsWith( "", "" ) returns true
Write a static method endsWith that inputs two Strings and returns a boolean. If the first...
Write a static method endsWith that inputs two Strings and returns a boolean. If the first input ends with the substring that is the second input, then the method returns true; otherwise, it returns false. For example, endsWith( "radar installation", "rad" ) returns false endsWith( "radar installation", "installation" ) returns true endsWith( "radar installation", "" ) returns true endsWith( "", "a" ) returns false endsWith( "", "" ) returns true
S = {(2,5,3)} and T = {(2,0,5)} are two clusters. Two clusters that S and T...
S = {(2,5,3)} and T = {(2,0,5)} are two clusters. Two clusters that S and T spans are L(S) and L(T) . Is the intersection of L (S) and L (T) a vector space? If yes, find this vector space. If no, explain why there is no vector space.
public class ArraySkills { public static void main(String[] args) { // *********************** // For each item...
public class ArraySkills { public static void main(String[] args) { // *********************** // For each item below you must code the solution. You may not use any of the // methods found in the Arrays class or the Collections classes // You must use Java's built-in Arrays. You are welcome to use the Math and/or Random class if necessary. // You MUST put your code directly beneath the comment for each item indicated. String[] myData; // 1. Instantiate the given...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT