#include <bits/stdc++.h>
using namespace std;

// iterative binary search

int binary_search_iterative(vector<string> arr, string key)
{
    int l = 0, r = arr.size();

    while (l <= r)
    {

        int mid = l + (r - l) / 2;
        if (arr[mid] == key)
            return mid;
        else if (arr[mid] < key)
            l = mid + 1;
        else
            r = mid - 1;
    }
    return -1;
}

// to print

// void print(vector<string> &a)
// {
//     for (auto it : a)
//         cout << it << " ";
//     cout << endl;
// }

int main()
{
    vector<string> arr = {"cat", "dog", "hant", "kat", "ase"};
    string key = "kat";

    // OR--> TAke input
    //  **************
    //  cout << "Enter size of the Input words : ";
    //  int n;
    //  cin >> n;

    // vector<string> arr(n);
    // cout << "Enter the words\n";
    // for (int i = 0; i < n; i++)
    // {
    //     cin >> arr[i];
    // }

    // cout << "Enter searching key\n";

    // // key there
    // string key;
    // cin >> key;

    // **************

    cout<< "Sorting the input list to ensure binary search works\n";

    sort(arr.begin(), arr.end());

    cout << "\nPrinting the sorted word list\n";

    // print array way-1
    // print(arr);

    // print array way-2
    // vector<string>::iterator it; // iterator declaration
    // for (it = arr.begin(); it != arr.end(); it++)
    //     cout << *it << " ";
    // cout << endl;

    // print array  way-3
    for (int i = 0; i < arr.size(); i++)
        cout << arr[i] << " ";
    cout << endl;

    int index = binary_search_iterative(arr, key);

    if (index == -1)
        cout << key << " not found\n";
    else
        cout << key << " found at index(0 based): " << index << endl;

    return 0;
}


Output

Enter size of the Input words : 5
Enter the words
cat
dog
panda
horse
snake
Enter searching key
panda
Sorting the input list to ensure binary search works

Printing the sorted word list
cat dog horse panda snake
panda found at index(0 based): 3 /// iterative
panda found at index(0 based): 3  /// recursive