Write a Java program to replace char in the string

Java program to replace char in the string, Obtain two strings from the user as input. Your program should modify the first string such that all the characters are replaced by a plus sign (+) except the characters which are present in the second string.

That is, if one or more characters of the first string appear in the second string, they will not be replaced by +. Return the modified string as output.

Note – ignore case.

Input and Output Format

  • Input consists of two strings with a maximum size of 100 characters.
  • Output consists of a single string.

Refer sample output for formatting specifications

Sample Input 1:

abcxyz

axdef

Sample Output 1:

a++ x++

Sample Input 2:

ABCDEF

feCBAd

Sample Output 2:

ABCDEF

Java program to replace char in the string

Following are the steps to replace char in the string:java program

  • Input two strings from the user.
  • Call display() method with both the string.
  • Inside the method, first, convert the string into lowercase and store it in s2 and s3.
  • Create an empty string buffer.
  • Iterate over the first string, and fetch each character of the string and store it in the c variable.
  • Returns the index within a second string of the specified character. If found append that character to string buffer otherwise appends “+” to the string buffer.
  • At last, return the string buffer.
import java.util.Scanner;
public class Main {
public static void main(String []args){
Scanner sc=new Scanner(System.in);
String n=sc.nextLine();
String n1=sc.nextLine();
System.out.println(display(n,n1));
}
 
public static String display(String s,String s1)
{
String s2=s.toLowerCase();
String s3=s1.toLowerCase();
StringBuffer sb=new StringBuffer();
for(int i=0;i<s.length();i++)
{
char c=s2.charAt(i);
if(s3.indexOf(c)==-1)
sb.append("+");
else
sb.append(s.charAt(i));
} return sb.toString();
 
}
}

Output

Replace a Character at Specified Index in a String in Java

Obtain a string from the user, and replace the character in the string at the specified index.

Input and Output Format

  • Input consists of a string, index. and character to replace
  • Output consists of a single string.

Refer sample output for formatting specifications

Sample Input 1:

Welcome to the world

8

h

Sample Output 1:

Welcome ho the world

Java program to replace character in a string

Following are the steps to replace a character at a specified position:

  • Input a string from the user. Also, input position and character should be replaced with the specified position.
  • Call the display() method with the inputs.
  • Inside the method, create a new String with 2 different substrings, one from beginning till the specific index – 1, the new character at the specific index, and the other from the index + 1.
  • At last, return this new string.
package com.company;

import java.util.Locale;
import java.util.Scanner;

public class PracticeSolution {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String n = scanner.nextLine();
        int position = scanner.nextInt();
        String c = scanner.next();
        System.out.println(display(n, position, c));
    }

    public static String display(String s, int position, String c) {

    String str = s.substring(0,position) + c + s.substring(position+1);
    return str;

    }
}

Output

Replace Multiple characters in a string in Java

Obtain a string and replace multiple characters in a string in Java. Let us use the following below approach to achieve this.

Following are the steps to replace multiple characters in a string:

  • First, set one string instr variable.
  • Now, use the replace() method and replace the target character sequence with the replacement.
  • At last, print the string.
package com.company;

import java.util.Locale;
import java.util.Scanner;

public class PracticeSolution {
    public static void main(String[] args) {
        String str = "Tea in the Tea Pot";
        str = str.replace("Tea", "Water");
        System.out.println(str);
    }
}

Output

In this way, we learn How to replace a char in string in Java.