
how to write a program for searching a string

Hi Friend,
Try this:
import java.util.*;
class SearchString{
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.println("Enter string: ");
String str = input.nextLine();
System.out.println("Enter word to search: ");
String word=input.next();
int index = str.indexOf(word);
if(index == - 1){
System.out.println("Not found");
}
else{
System.out.println(word+"found at index " + index);
}
}
}
Thanks
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.