0% found this document useful (0 votes)
7 views2 pages

Linear Search Record

The document contains C++ source code for a linear search algorithm that searches a string array for a target string. The code defines a linear_search function, takes user input for the array size and elements, calls linear_search to search for a target string, and prints whether it was found and the index if so.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Linear Search Record

The document contains C++ source code for a linear search algorithm that searches a string array for a target string. The code defines a linear_search function, takes user input for the array size and elements, calls linear_search to search for a target string, and prints whether it was found and the index if so.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

SOURCE CODE:

#include<iostream>
#include<cstring>
using namespace std;
int n;
int linear_search(int n, string a[], string search)
{
for(int i = 0; i < n; i++)
{
if(a[i] == search)
{
return i;
}
}
return -1;
}
int main()
{
cout<<"\nEnter Total Number of Data: ";
cin>>n;
string a[n], search;
cout<<"\nEnter "<<n<<" Data: \n";
for(int i = 0; i < n; i++)
{
cin>>a[i];
}
cout<<"\nEnter Data to be Searched: ";
cin>>search;
int result = linear_search(n, a, search);
if(result == -1)
{
cout<<"\nSearch Data not found!";
}
else
{
cout<<"\nSearch Data found! and Present at the index \""<<result<<"\"";
}
return 0;
}
INPUT AND OUTPUT:

You might also like