Write a Java Program to Delete or Remove Words from String without Function

By | 09.03.2017

Java Program to Delete or Remove Words


Write a Java Program to Delete or Remove Words from String without using Inbuilt Function. For delete any particular word from the string  in Java Programming, first, you have to ask to the user to enter the string,then second ask to enter the any word present in the string 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.

Example –

Input– This is the sample input from the user.
Word to be deleted– the
Output– This is sample input from user.

Since, We have two methods to delete vowels from the string : –

First method that we already discussed in the previous post that is to Program to Remove or Delete words using Inbuilt Function .

Second method is shortcut method that uses the manual method to replace all the words from the string and display the screen on the screen

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

Here is source code of the Java Program to remove given word from a string. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.


SOURCE CODE : :


import java.io.IOException;

import java.util.Scanner;

public class Remove_words {

public static void main(String[] args) throws IOException {

    String[] s;
    String sentence, word;
    
    System.out.println("Enter the sentence");
    
    Scanner sc = new Scanner(System.in);

    sentence = sc.nextLine();

    System.out.println("Enter the word to be deleted");
    word = sc.nextLine();

    String finalSentence = "";

    s = sentence.split(" ");

    for (String item : s) {
        if (word.equals(item)) {
            
        }
         else {
            finalSentence += item + " ";
        }
    }

    System.out.println("final sentence is :: " + finalSentence);
    sc.close();

}
}

OUTPUT : :


Enter the sentence
This is the test string
Enter the word to be deleted
test
final sentence is :: This is the string
1.5 2 votes
Article Rating
Category: Java Programming String Programs Tags:

About Tunde A

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments