Spread the love

Ashok is developing a text processing utility that allows users to insert a substring into an original string at a specified position. The program takes input for the original string, the substring to insert, and the position at which to insert the substring.

Write a program to help Ashok using the methods of the String Builder class.

Input format :

The first line of input contains a string representing the original string.

The second line of input contains a string representing the substring to insert.

The third line of input contains a integer representing the index position to insert the substring (index starts from 0).

Output format :

The output prints a string representing the modified string after inserting the substring.

Refer to the sample output for formatting specifications.

Code constraints :

The given test cases fall under the following constraints:

0 ≤ index position ≤ 50

Sample test cases :

Input 1 :

HelloWorld
New
6

Output 1 :

HelloWNeworld

Input 2 :

Hello
World
0

Output 2 :

WorldHello

Input 3 :

This is an place
amazing
11

Output 3 :

This is an amazingpl

import java.util.Scanner;

public class StringReplacer {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Read input string
        String inputString = scanner.nextLine();
        
        // Read the target character to be replaced
        char targetChar = scanner.next().charAt(0);
        
        // Read the replacement character
        char replacementChar = scanner.next().charAt(0);
        
        // Replace all occurrences of targetChar with replacementChar
        String modifiedString = inputString.replace(targetChar, replacementChar);
        
        // Print the modified string
        System.out.println(modifiedString);

        scanner.close();
    }
}


Sophia is working on a program to combine two strings into a new one by concatenating their characters while ensuring that no character is repeated in the final result.

Write a program to merge two strings into one by retaining only unique characters in the order they first appear String builder class.

Input format :

The first line contains a string representing the firstString which consist of alphanumeric characters, spaces, and symbols.

The second line contains a string representing the secondString, with the same format as firstString.

Output format :

The output prints a single string containing all unique characters from firstString and secondString in the order of their first appearance.

Refer to the sample output for format specifications.

Code constraints :

In the given scenario, the test cases fall under the following constraints:

1 ≤ Length of each string ≤ 250 characters

The input strings are case-sensitive.

Sample test cases :

Input 1 :

heLlo@123
world@456

Output 1 :

heLlo@123wrd456

Input 2 :

aabbccddeeffgghhiijj
jjiihhggffeeddccbbaa

Output 2 :

abcdefghij

Input 3 :

Harry Potter
Child Play

Output 3 :

Hary PoteChild
import java.util.Scanner;
import java.util.LinkedHashSet;

class StringManipulation {
    public static String concatenateUnique(String str1, String str2) {
        // Concatenate the two strings
        String combined = str1 + str2;
        
        // Use LinkedHashSet to remove duplicates while preserving order
        LinkedHashSet<Character> uniqueChars = new LinkedHashSet<>();
        StringBuilder result = new StringBuilder();
        
        for (char c : combined.toCharArray()) {
            if (uniqueChars.add(c)) { // Add only if it's not a duplicate
                result.append(c);
            }
        }
        
        return result.toString();
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Read input strings
        String firstString = scanner.nextLine();
        String secondString = scanner.nextLine();

        // Call concatenateUnique method
        String concatenatedString = StringManipulation.concatenateUnique(firstString, secondString);
        
        // Print the result
        System.out.println(concatenatedString);

        scanner.close();
    }
}

Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top