0% found this document useful (0 votes)
5 views9 pages

String Array Questions

The document contains several Java programs that demonstrate various search and sorting algorithms on string arrays. It includes implementations for linear search, binary search, bubble sort, and selection sort, as well as a sorting program that organizes student names based on their heights in descending order. Each program is structured with input prompts, processing logic, and output displays.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views9 pages

String Array Questions

The document contains several Java programs that demonstrate various search and sorting algorithms on string arrays. It includes implementations for linear search, binary search, bubble sort, and selection sort, as well as a sorting program that organizes student names based on their heights in descending order. Each program is structured with input prompts, processing logic, and output displays.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

String Array Questions

Question 1.
Write a program to perform Linear Search on String array of 10
values.
Answer:
import [Link].*;
class LinearSearchString
{
public static void main()
{
Scanner sc = new Scanner([Link]);
String a[] = new String[10];
int i,pos=-1;
String nm;
for(i=0;i<10;i++)
{
[Link]("Enter name :");
a[i]=[Link]();
a[i]=a[i].toUpperCase();
}
[Link]("Enter name to be searched :");
nm=[Link]();
for(i=0;i<10;i++)
{
if([Link](a[i]))
{
pos=i;
break;
}
}
if(pos==-1)
[Link]("Name not found");
else
[Link]("Name found at position ="+(pos+1));
}
}
Question 2. Binary Search on Strings
Answer:
import [Link].*;
class BinarySearchString
{
public static void main()
{
Scanner sc = new Scanner([Link]);
String
a[]={"Ahmedabad","Bhopal","Chennai","Delhi","Mumbai","Ranc
hi"};
int l=0,u=[Link]-1,mid,pos=-1;
String s;
[Link]("Enter Search City :");
s=[Link]();
while(l<=u)
{
mid=(l+u)/2;
if([Link](a[mid]))
{
pos=mid;
break;
}
else
if([Link](a[mid])<0)
u=mid-1;
else
l=mid+1;
}
if(pos==-1)
[Link]("Value not found");
else
[Link]("Value found at position ="+(pos+1));
}
}

Question 3. Write a program to accept 10 names and sort


them using bubble sort.
Answer:
import [Link].*;
class BubblesortString
{
public static void main()
{
Scanner sc = new Scanner([Link]);
String a[] = new String[10];
int i,j;
String t;
for(i=0;i<10;i++)
{
[Link]("Enter city name :");
a[i]=[Link]();
}
//process
for(i=0;i<[Link];i++)
{
for(j=0;j<[Link]-1-i;j++)
{
if(a[j].compareToIgnoreCase(a[j+1])>0)
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}//if ends
}//j ends
}//i ends
//output
[Link]("Sorted Array");

for(i=0;i<10;i++)
{
[Link](a[i]);
}
}
}
Question 4. Write a program to accept 10 names and sort
them using selection sort.
import [Link].*;
class SelectionSortString
{
public static void main()
{
Scanner sc = new Scanner([Link]);
String a[]=new String[10];
int i,j,pos;
String t,m;
//input
for(i=0;i<10;i++)
{
[Link]("Enter City name: ");
a[i]=[Link]();
}
//process
for(i=0;i<[Link];i++)
{
m=a[i];
pos=i;
for(j=i+1;j<[Link];j++)
{
if([Link](a[j])>0)
{
m=a[j];
pos=j;
}//if ends
}//j ends
t=a[i];
a[i]=a[pos];
a[pos]=t;
}//i ends
//output
[Link]("Sorted Array");
for(i=0;i<10;i++)
{
[Link](a[i]);
}
}
}
Question 5. Write a program to accept 10 students names
with the height in two different arrays and sort them on height
in descending order.
Answer:
import [Link].*;
class Bubblesortnmheight
{
public static void main()
{
Scanner sc = new Scanner([Link]);
String nm[] = new String[10];
double ht[] = new double[10];
int i,j;
for(i=0;i<10;i++)
{
[Link]("Enter name :");
nm[i]=[Link]();
[Link]("Enter height :");
ht[i]=[Link]();
}
//process
for(i=0;i<10;i++)
{
for(j=0;j<10-1-i;j++)
{
if(ht[j]<ht[j+1])
{
double t=ht[j];
ht[j]=ht[j+1];
ht[j+1]=t;
String temp=nm[j];
nm[j]=nm[j+1];
nm[j+1]=temp;
}//if ends
}//j ends
}//i ends
//output
[Link]("Sorted Array in descending order");
[Link]("Name Height");
for(i=0;i<10;i++)
{
[Link](nm[i]+"\t\t"+ht[i]);
}
}
}

You might also like