Code is copied!
Define a class to accept the names of 10 students in an array and check for the existence of the given name in the array using linear search, if found print the position of the name, if not found print the appropriate message. Also print the names which begins with the word “SRI”.
Solution:
import java.util.Scanner;
class Students
{
public static void main (String args[])
{
String names[]=new String[10];
Scanner sc = new Scanner(System.in);
for(int i=0; i< 10; i++)
{
System.out.println("Enter the "+(i+1)+" name");
names[i]=sc.nextLine();
}
System.out.println("Enter the name you want to search");
String search = sc.nextLine();
int pos=-1;
for (int i=0; i < names.length; i++)
{
if(search.equalsIgnoreCase(names[i]))
{
pos=i;
break;
}
}
if(pos!=-1)
System.out.println("Name is found at position " +(pos+1));
else
System.out.println("Name is not found");
System.out.println("
NAMES STARTING WITH SRI");
for (int i=0; i < names.length; i++)
{
if(names[i].startsWith("SRI"))
{
System.out.println(names[i]);
}
}
}
}