Created
February 25, 2025 08:51
-
-
Save ProfAndreaPollini/b979dfe79db38d64a02cd12e7317a3ba to your computer and use it in GitHub Desktop.
occorrenze di una stringa in una linea di un file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <fstream> | |
#include <iostream> | |
#include <string> | |
int main() { | |
std::ifstream in_file{"dati.txt"}; | |
std::string line,search_string; | |
std::cout << "Inserisci stringa da cercare> "; | |
std::cin >> search_string; | |
//in_file >> line; | |
std::getline(in_file,line); | |
bool found = false; | |
for (int i = 0; i< line.size();i++) { | |
auto s = line.substr(i,search_string.size()); | |
if (s == search_string) { | |
found = true; | |
} | |
} | |
if (found) { | |
std::cout << "trovata!\n"; | |
} else { | |
std::cout << "non trovata\n"; | |
} | |
// contare quante volte si trova la | |
// sottostringa | |
int occorrenze = 0; | |
for (int i = 0; i< line.size();i++) { | |
auto s = line.substr(i,search_string.size()); | |
if (s == search_string) { | |
occorrenze++; | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment