Reputation: 255
I want to be able to search an array of strings in C++. I have this data:
"Foo Becky, 924-334-2514",
"Becky Warren, 555-1223",
"Geri Palmer, 555-8787",
"Ron Palmer, 555-2783"
If the user types Bec
, the program finds the name Foo Becky, 924-234-2314
. If the user types Palmer
, then the program should show Geri Palmer, 555-8787
and Ron Palmer, 555-2783
Here is what I have so far:
#include <iostream>
#include <string>
using namespace std;
int main(){
int n;
string search;
while(1){
cout << "How many data you want to input: "<< endl;
cin >> n;
cin.ignore(1000, 10);
if(n > 0 && n < 20){
break;
}
cout << "Number of data can not be negative or more than 20. "<< endl;
}
string* data = new string[n];
for(int i=0; i< n; i++){
cout << "Enter [First Name] [Last Name], [Phone-Number] and then hit "
<< "enter." << endl << "e.g: Foo Becky, 925-245-413"<< endl;
getline(cin,data[i]);
cout << endl;
}
cout << "Enter what you want to search for: "<< endl;
getline(cin, search);
for(int i =0; i< n; i++){
if(search == data[i]){
cout << data[i]<< endl;
}
}
delete [] data;
return 0;
}
How do I search an array of strings in C++?
Upvotes: 0
Views: 31742
Reputation: 153882
How to search an array of strings example in C++:
This is the brute force search method. Brute force means means we step through the entire string array and at each index of the array we search for our matching string.
#include<iostream>
#include<string>
using namespace std;
int main(){
//Create a structure to hold the data:
string data[] = {"pidgeon", "abcd", "1234", "%^*#"};
//Get the length of the array.
int size = 4;
//loop through all the items and print them if they match
string matchString = "bc";
for(int x = 0; x < size; x++){
if (data[x].find(matchString, 0) != std::string::npos){
cout << data[x] << endl;
}
}
}
Above code prints:
abcd
You should be verbalizing the above code from top to bottom in your head like this:
An array of string called data is initialized to contain 4 elements and given four values pidgeon
, abcd
, 1234
and %^&#
. An int variable called size is created which represents the number of elements in the string array. A string variable called matchString is created which contains the string 'bc'.
The for loop index starts at position zero and increments by 1 until it reaches one less than the size of the array. So the for loop goes through: 0, 1, 2, 3. The first value of x is 0. The if statement resolves data[0] to be pidgeon. The find method is applied against that string and two parameters are passed in. The string to be matched, and the position of the first character in the string to be considered in the search (0).
if 'bc' exists within 'pidgeon' then it will return the position of the first character of the first match, otherwise it will print std::string:npos which is the maximum value for size_t designating not found.
bc does not exist in pidgeon so it skips the interior of the for loop. The for loop continues to index position 1. bc is contained in abcd. So that string is printed. When all the items are searched, the for loop ends and the program completes.
Upvotes: 0
Reputation: 130
You should use find, as A4L already mentioned. I just want to add that your use of cin.ignore will not work well if bad value entered. you need
cin.clear()
also. see this link for more details.
Upvotes: 1
Reputation: 17595
You have to use the find method of std::string
. This function return the start postion of string searched for in the string searched in. If no match was found it returns npos witch is actually just -1
.
if(data[i].find(search, 0) != std::string::npos);
{
cout << data[i]<< endl;
}
Upvotes: 1