Program to find specific word in given string and replace it with new string in Java

 



Code:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        String str, old, nword;
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a string: ");
        str = sc.nextLine();
        System.out.print("Enter word you want to replace: ");
        old = sc.nextLine();
        System.out.print("Enter new word: ");
        nword = sc.nextLine();

        str = str.replace(old, nword);

        System.out.println("\nNew string is: "+str);
    }

}

Output:

AJ Blogs

Hello everyone, My name Arth and I like to write about what I learn. Follow My Website - https://sites.google.com/view/aj-blogs/home

Post a Comment

Previous Post Next Post
Best Programming Books

Facebook

AJ Facebook
Checkout Our Facebook Page
AJ Blogs
Checkout Our Instagram Page