Untitled
unknown
c_cpp
2 years ago
998 B
11
Indexable
#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");
}
Editor is loading...
Leave a Comment