Computer Science, asked by anilakokapeta, 2 months ago

Write a java program to find the given word is contained in a given sentence or not.

Note:

The second input is contained in the first input, then print “ is contained in a sentence”. Else print “ is not contained in a sentence”.

Sample Input1:
Creativity is always having fun

having
Sample Output1:

having is contained in a sentence

Sample Input2:

we know one of the most places

fun

Sample Output2:

fun is not contained in a sentence

//Need answer for this guys help me out!

Answers

Answered by StephenKJ
1

import java.util.Scanner;

public class check_words {

   public static void main(String[] args){

       String sentence;

       String word;

       String w = "";

       int len, i, f = 0;

       Scanner obj= new Scanner(System.in);

       System.out.println("Enter the sentence: ");  // enter the sentence

       sentence = obj.nextLine();

       sentence = sentence + " ";         // add space at the end of sentence

       len = sentence.length();

       System.out.println("Enter the word to be searched: ");  // enter the word

       word = obj.nextLine();

       word = word.trim();

       for(i=0;i<len;i++){

           if(sentence.charAt(i)!=' '){

               w = w + sentence.charAt(i);  // to seperate each word

           }

           else{

               if(w.equals(word)){     // check whether the word is the input word

                   System.out.println(w+" is contained in a sentence");

                   f=1;

                   break;

               }

               w="";      // to reset w to ""

           }

       }

       if(f==0)

           System.out.println(word+" is not contained in a sentence ");

   }

}

I have also attached the output

Attachments:
Answered by azitabience
4

Answer:

all test cases passed!

Explanation:

Attachments:
Similar questions