In this tutorial you will learn about the Java Program to Check if a word is present in sentence and its application with practical example.
Java Program to Check if a word is present in the sentence
In this tutorial, we will learn to create a Java program that will Check if a word is present in a sentence in Java programming
Prerequisites
Before starting with this tutorial, we assume that you are the best aware of the following Java programming topics:
- Operators in Java Programming.
- Basic Input and Output function in Java Programming.
- Basic Java programming.
- For loop in Java programming.
- Conditional Statements in Java programming.
What is a string?
As we all know, the String is a collection of characters and words. The word is a collection of alphabets. For string, only one variable is declared which can store multiple values. The String can consist of all the type able data, it means Digits, Alphabets, Symbols, etc.
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 12 13 |
1. Declaring the variables for the program. 2. Taking the input string from the user. 3. Taking the input word from the user that needs to be searched. 4. Passing those variables to for loop. 5. Using conditional statements for the program to find the word occurrence of in that sentence. 6. Printing the result position. 7. End the program. |
Check if a word is present in the sentence
In this program, first, we will declare the input string in the program. Then we will declare the word to be searched in the sentence. Then will find the occurrence of that word in that string. Printing the result if that string is present in the sentence or not.
Let us take the example program from the below code to search for the occurrence of that word.
Program:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
import java.util.*;//Importing the util package in the program. class Main { //Main function body:- public static void main(String[] args) { //Declaring the svariables for the program. // creating a string for the program. String txt = "This is the test stentence for the w3adda program"; String str1 = "is"; String str2 = "for"; System.out.println(" Default string =" + txt); System.out.println(" Searching the 'for' in sentence "); System.out.println(" Searching the 'is' in sentence "); // check if str1 is present in txt // using indexOf() int result = txt.indexOf(str1); if(result == -1) { System.out.println(str1 + " not is present in the string."); } else { System.out.println(str1 + " is present in the string."); } // check if str2 is present in txt // using indexOf() result = txt.indexOf(str2); if(result == -1) { //printing the ouput string to the user. System.out.println(str2 + " is not present in the string."); } else { System.out.println(str2 + " is present in the string."); } } } |
Output:-
In the above program, we have first initialized the required variable.
- txt = It will hold the string value.
- str1 = It will hold the string value.
Searching the first string in the sentence using a conditional statement.
Searching the second word in the Sentence.
Printing output words are present in the sentence or not.