codescracker


java

Java Program to Delete Words from Sentence



« Previous Program Next Program »


Delete Words from Sentence

To delete any particular word from the string/sentence in Java Programming, first, you have to ask to the user to enter the string/sentence, second ask to enter the any word present in the string/sentence to delete that word from the string. After asking the two, now check for the presence of that word and perform the deletion of that word from the sentence as shown in the following program.

Java Programming Code to Delete Words from Sentence

Following Java Program ask to the user to enter a string/sentence then ask to enter a word to be delete from the string, then display the new string/sentence after deleting the given word:

/* 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);       
   }
}

When the above Java Program is compile and executed, it will produce the following output:

Java Program delete word from sentence

Same Program in Other Languages

You may also like to learn and practice the same program in other popular programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test