Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
885 B
2
Indexable
Never
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

int searchFromPosition(const std::vector<std::string>& arr, const std::string& target, int startPos) {
    int size = static_cast<int>(arr.size());
    for (int i = 0; i < size; ++i) {
        int index = (startPos + i) % size;
        std::cout << index << "\n";
        if (arr[index] == target) {
            return index;
        }
    }
    return -1; 
}

int main() {
    std::vector<std::string> array = {"apple", "banana", "orange", "grape", "apple"};
    std::string searchStr = "orange";
    static int startPosition = 2;

    int result = searchFromPosition(array, searchStr, startPosition);
    if (result != -1) {
        std::cout << "Found '" << searchStr << "' at index: " << result;
    } else {
        std::cout << "Could not find '" << searchStr << "' in array.";
    }

    return 0;
}