Question

Use java and recursion! Repeat substrings of the given String required number of times Given a...

Use java and recursion!

Repeat substrings of the given String required number of times

Given a string str, the task is to repeat every substring of the string X number of times where X is the number composed of the consecutive digits present just after the substring in the original string.

For example, if str = “1g2e1ks1” then the resultant string will be “geeks”.

Input: str = “2a3bd”
Output: aabbbddd

Homework Answers

Answer #1

If you have any queries please comment in the comments section I will surely help you out and if you found this solution to be helpful kindly upvote.

Solution :

Code :

import java.util.Scanner;
public class HelloWorld
{
// main function
public static void main(String []args)
{
// declare variables
int i,j,a,k,p;
// declare an object of Scanner class
Scanner sc = new Scanner(System.in);
// input string
String s = sc.nextLine();
// declare an empty string for the answer
String ans = "";
// iterate through the loop
for(i=0;i<s.length();i++)
{
// check if the character is a digit
if(Character.isDigit(s.charAt(i)))
{
// declare an empty string
String temp="";
// now iterate from (i+1)th index to last index
for(j=i+1;j<s.length();j++)
{
// take all the letters to the temporary string
if(Character.isLetter(s.charAt(j)))
{
temp=temp+s.charAt(j);
}
// if the character is not a letter then break
else
{
break;
}
}
// take the numeric value of ith index
a = Character.getNumericValue(s.charAt(i));
// iterate over the temporary string
for(k=0;k<temp.length();k++)
{
// add every character to the answer string 'a' times
for(p=0;p<a;p++)
{
ans=ans+temp.charAt(k);
}
}
// make (j-1)th index as the ith index
i=j-1;
}
}
// print the result
System.out.println(ans);
}
}

Output :

Know the answer?
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
Similar Questions
In Java. Write a program that reads-in a times table-number. The program, using this table-number will...
In Java. Write a program that reads-in a times table-number. The program, using this table-number will produce the following report (as shown). The first item in the report is a number with starting value 1. Second column is the word “ X ” (representing the times symbol). Third column is the table-number (itself). Following is an equal sign “ = “ (representing a result). Last column is the result of the operation for that line or row which is the...
---------------- Overview: ---------------- This lab will consist of several independent exercises that must all use recursion...
---------------- Overview: ---------------- This lab will consist of several independent exercises that must all use recursion to solve various problems. Some problems will have fairly short solutions, but declaring and using classes/objects is still required. Some could benefit from having some kind of data structure being passed between calls. Feel free to use your LinkedList if you think it could help. Some solutions will be a mix of iteration and recursion, but all solution need to be recursive in some...
Objectives:The focus of this assignment is to create and use a recursive method given a moderately...
Objectives:The focus of this assignment is to create and use a recursive method given a moderately difficult problem. Program Description: This project will alter the EmployeeManager to add a search feature, allowing the user to find an Employee by a substring of their name. This will be done by implementing the Rabin-Karp algorithm. A total of seven classes are required. Employee (From previous assignment) HourlyEmployee (From previous assignment) SalaryEmployee (From previous assignment) CommissionEmployee (From previous assignment) EmployeeManager (Altered from previous...
/* This program should check if the given integer number is prime. Reminder, an integer number...
/* This program should check if the given integer number is prime. Reminder, an integer number greater than 1 is prime if it divisible only by itself and by 1. In other words a prime number divided by any other natural number (besides 1 and itself) will have a non-zero remainder. Your task: Write a method called checkPrime(n) that will take an integer greater than 1 as an input, and return true if that integer is prime; otherwise, it should...
**please write code with function definition taking in input and use given variable names** for e.g....
**please write code with function definition taking in input and use given variable names** for e.g. List matchNames(List inputNames, List secRecords) Java or Python Please Note:    * The function is expected to return a STRING_ARRAY.      * The function accepts following parameters:      *  1. STRING_ARRAY inputNames      *  2. STRING_ARRAY secRecords      */ Problem Statement Introduction Imagine you are helping the Security Exchange Commission (SEC) respond to anonymous tips. One of the biggest problems the team faces is handling the transcription of the companies reported...
What would the pseudocode be for this? The code for this has already been answered. I...
What would the pseudocode be for this? The code for this has already been answered. I need the pseudocode. Credit card numbers typically consist of 13, 15, or 16 digits. For example, 4690 3582 1375 4657 is a hypothetical credit card number. The first digit designates the system. In (3.1.1), the first digit, 4, shows that the card would be a Visa card. The following digits specify other information such as the account number and bank number. (The precise meaning...
JAVA Bike shares are becoming increasingly common in cities in the United States. Commuters have been...
JAVA Bike shares are becoming increasingly common in cities in the United States. Commuters have been using bike sharing as a method of transportation for the flexibility, cost savings, exercise, and a myriad of other benefits. While being quite successful, many bike shares are still relatively new and have room for optimization. To help with the optimization process, we want to analyze bike share data from the city of Los Angeles. The first step of the process is to collect...
Use Python 3.8: Problem Description Many recipes tend to be rather small, producing the fewest number...
Use Python 3.8: Problem Description Many recipes tend to be rather small, producing the fewest number of servings that are really possible with the included ingredients. Sometimes one will want to be able to scale those recipes upwards for serving larger groups. This program's task is to determine how much of each ingredient in a recipe will be required for a target party size. The first inputs to the program will be the recipe itself. Here is an example recipe...
USE C++!!!! Encryption and Decryption are two cryptographic techniques. Encryption is used to transform text to...
USE C++!!!! Encryption and Decryption are two cryptographic techniques. Encryption is used to transform text to meaningless characters, and decryption is used to transform meaningless characters into meaningful text. The algorithm that does the encryption is called a cipher. A simple encryption algorithm is Caesar cipher, which works as follows: replace each clear text letter by a letter chosen to be n places later in the alphabet. The number of places, n, is called the cipher key. For example, if...
python programming Question #4: # Years ago the Romans used a different system to represent numbers....
python programming Question #4: # Years ago the Romans used a different system to represent numbers. # Instead of using the digits (0, 1, 2, 3, 4, 5, 6, etc.), the Romans # formed numbers by joining combinations of the characters # (I, V, X, L, C, D, and M). # Roman Numeral characters and their integer values are: # I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, and M...