Write a program for:
Searching a word present in a sentence or not, if present print its location with a successful note.
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");
}
}
Comments
Leave a comment