Untitled

mail@pastecode.io avatar
unknown
c_cpp
a month ago
998 B
2
Indexable
Never
#include <iostream>
#include <print>


int main()
{
    std::string text;
    std::string search_word;
    
    std::println("Insira o texto:");
    std::cin >> text;

    std::println("Insira a palavra de busca:");
    std::cin >> search_word;
    
    int counter = 0;
    for (int i = 0; i < text.size();) {
        char c = text.at(i);
        
        if (c == search_word.at(0)) {
            /* Pra ter crtz que ainda da pra percorrer mais casas */
            if (text.size() < i + search_word.size()) { break; }

            bool has_found = true;

            for (int j = 0; j < search_word.size(); j++) {
                if (text.at(j + i) != search_word.at(j)) {
                    has_found = false;
                    break;
                }
            }

            if (has_found) {
                counter++;
                i += search_word.size() - 1; //jump
            }
        } 
        i++;
    }
    std::println("Ocorrencias: {}\n", counter);
    system("pause");
}
Leave a Comment