Search a Word | Write a program in java to search a word in a sentence | ISC Computer Science | ICSE Computer Applications
Algorithm
- Start
- sent=null
- word=null
- search=null
- k:=0
- Print "Enter a sentence"
- Read sent
- Print "Enter a word to be searched"
- Read search
- search.trim()
- sent.trim()
- sent=sent+" "
- k=0
- If sent.compareTo(" ")!=-1, go to step 15 or else go to step 20
- word=sent.substring(0, sent.indexOf(' '))
- sent=sent.substring(sent.indexOf(' ')+1, sent.length())
- If word.equalsIgnoreCase(search)==true, go to step 18 or else go to step 19
- k++
- Go to step 14
- If (k!=0), go to step 21 or else go to step 22
- Print "The word is present in the given sentence". Go to step 23
- Print "The word is not present in the given sentence"
- Stop
Program
Buffered Reader
import java.io.*;
class Search
{
String sent, word, search;
int k;
void accept() throws IOException
{
BufferedReader br=new BufferedReader (new InputStreamReader (System.in));
System.out.println("Enter a sentence");
sent=br.readLine();
System.out.println("Enter a word to be searched");
search=br.readLine();
search.trim();
sent.trim();
sent=sent+" ";
k=0;
}
void isPresent()
{
while(sent.compareTo(" ")!=-1)
{
word=sent.substring(0, sent.indexOf(' '));
sent=sent.substring(sent.indexOf(' ')+1, sent.length());
if(word.equalsIgnoreCase(search)==true)
{
k++;
}
}
}
void display()
{
if(k!=0)
{
System.out.println("The word is present in the given sentence");
}
else
{
System.out.println("The word is not present in the given sentence");
}
}
void main() throws IOException
{
accept();
isPresent();
display();
}
}
Scanner
import java.util.*;
class Search
{
String sent, word, search;
int k;
void accept()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a sentence");
sent=sc.nextLine();
System.out.println("Enter a word to be searched");
search=sc.nextLine();
search.trim();
sent.trim();
sent=sent+" ";
k=0;
}
void isPresent()
{
while(sent.compareTo(" ")!=-1)
{
word=sent.substring(0, sent.indexOf(' '));
sent=sent.substring(sent.indexOf(' ')+1, sent.length());
if(word.equalsIgnoreCase(search)==true)
{
k++;
}
}
}
void display()
{
if(k!=0)
{
System.out.println("The word is present in the given sentence");
}
else
{
System.out.println("The word is not present in the given sentence");
}
}
void main()
{
accept();
isPresent();
display();
}
}
Documentation
Variable Description
Data Type | Variable Name | Description |
String | sent | To store the sentence |
String | word | To store a word from the sentence |
String | search | To store the word to be searched |
int | k | To act as a counter variable |
Method Description
Return Type | Method Name | Description |
void | accept() | To accept data from the user |
void | isPresent() | To check if the given word is present in the sentence or not |
void | display() | To display the result of the program |
void | main() | To act as an entry gateway for the compiler. |
Output
Share with all ISC Computer Science Students you know.
Comment below with all your program requests and blog suggestions.
Follow us on social media by clicking on these links.
Instagram : @ISCJavaCode
Twitter: @ISCJavaCode
Quora: ISC Java Code
Reddit: @ISCJavaCode
Stay Healthy
Stay Happy
Stay Helpful
Comments
Post a Comment