Search for a String input by the user using linear search technique
Define a class to declare a String array of size n and accept elements into the array. Search
for a String input by the user using linear search technique, display the string along with
its position if it is found, otherwise display the message “STRING NOT FOUND”.
import java.util.;
class Array_P1
{
public static void main(String[]args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the array size");
int n=sc.nextInt();/** stores the array size/
String s[]=new String[n];/** stores 10 strings*/
System.out.println("Enter "+n+" strings");
for(int i=0;i<s.length;i++)
{
s[i]=sc.nextLine();
}
System.out.println("Enter a string to search");
String search=sc.nextLine();
boolean found=false;
for(int i=0;i<s.length;i++)
{
if(search.equalsIgnoreCase(s[i]))
{
found=true;
System.out.println(search);
System.out.println("Index position is "+i);
break;
}
}
if(found==false)
System.out.println("STRING NOT FOUND");
}
}
FOR CODE ,
CLICK : https://onecompiler.com/java/3y4eee68h ( TO FIND OUTPUT)