Java Code to Delete Words from Sentence - Computer Programming

Latest

C C++ Java Python Perl Programs Examples with Output -useful for Schools & College Students

Monday, September 25, 2017

Java Code to Delete Words from Sentence

Write a Java Code to Delete Words from Sentence

Program:
/* Java Program Example - Delete Word from Sentence */

import java.util.Scanner;

public class JavaProgram
{
public static void main(String args[])
{
String strOrig, word;
Scanner scan = new Scanner(System.in);

System.out.print("Enter a String : ");
strOrig = scan.nextLine();

System.out.print("Enter a Word to be Delete from the String : ");
word = scan.nextLine();

System.out.print("Deleting all '" + word + "' from '" + strOrig + "'...\n");
strOrig = strOrig.replaceAll(word, "");

System.out.print("Specified word deleted Successfully from the String..!!");

System.out.print("\nNow the String is :\n");
System.out.print(strOrig);
}

}

OUTPUT:
Enter a String : learn the program for U
Enter a Word to be Delete from the String : r
Deleting all 'r' from 'learn the program for U'...
Specified word deleted Successfully from the String..!!
Now the String is :
lean the pogam fo U


No comments:

Post a Comment