Search a Word | Write a program in java to search a word in a sentence | ISC Computer Science | ICSE Computer Applications

Search a Word

Algorithm

  1.   Start
  2. sent=null
  3. word=null
  4. search=null
  5. k:=0
  6. Print "Enter a sentence"
  7. Read sent
  8. Print "Enter a word to be searched"
  9. Read search
  10. search.trim()
  11. sent.trim()
  12. sent=sent+" "
  13. k=0
  14. If sent.compareTo(" ")!=-1, go to step 15 or else go to step 20
  15. word=sent.substring(0, sent.indexOf(' '))
  16. sent=sent.substring(sent.indexOf(' ')+1, sent.length())
  17. If word.equalsIgnoreCase(search)==true, go to step 18 or else go to step 19
  18. k++
  19. Go to step 14
  20. If (k!=0), go to step 21 or else go to step 22
  21. Print "The word is present in the given sentence". Go to step 23
  22. Print "The word is not present in the given sentence"
  23. 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 TypeVariable NameDescription
 Stringsent To store the sentence  
String word To store a word from the sentence 
 Stringsearch To store the word to be searched 
 intTo act as a counter variable 

Method Description

 Return TypeMethod NameDescription
 voidaccept() To accept data from the user 
 voidisPresent() To check if the given word is present in the sentence or not 
 voiddisplay() To display the result of the program 
 voidmain() To act as an entry gateway for the compiler. 

Output


Search a Word 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
Reddit: @ISCJavaCode
Pinterest: @ISCJavaCode
Facebook: @ISCJavaCode
LinkedIn : ISC Java Code 


Stay Healthy
Stay Happy
Stay Helpful



Comments

You might also like:

Conversion of Complex Sentences to Simple Sentences

Emirp Number | Write a program in Java to print an Emirp Number. | ISC Computer Science | ICSE Computer Applications

Conversion of Complex Sentences to Compound Sentences