Answer to Question #264363 in C++ for 1\/\/0

Question #264363

Write a program for:

Searching a word present in a sentence or not, if present print its location with a successful note.


1
Expert's answer
2021-11-12T17:37:22-0500
import java.util.Scanner;
class Main{
    static boolean isPresent(String st, String w)
    {
    	String []s = st.split(" ");
    	for ( String temp :s)
    	{
    		if (temp.compareTo(w) == 0)
    		{
    			return true;
    		}
    	}
    	return false;
    }


public static void main(String[] args)
{
	String s;
	String w;
	Scanner in=new Scanner(System.in);
	System.out.println("Enter a sentence: ");
	s=in.nextLine();
	System.out.println("Enter a word to search in the sentence: ");
	w=in.next();
    
	if (isPresent(s, w))
		System.out.print("The word is present in the sentence");
	else
		System.out.print("The word is NOT present in the sentence");


}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS