Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions sem2/BelyaevNA/homework_3/StringForge.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>

int main() {
std::ifstream input("input.txt");
if (!input) {
std::cerr << "Failed to open input.txt\n";
return 1;
}

std::vector<std::string> lines;
std::string line;
while (std::getline(input, line)) {
lines.push_back(line);
}
input.close();

std::vector<std::string> original = lines;

size_t threshold;
std::string search_word;

std::cout << "Enter length threshold: ";
std::cin >> threshold;
std::cout << "Enter word to search: ";
std::cin >> search_word;

auto remove_short = [threshold](std::vector<std::string>& vec) {
vec.erase(std::remove_if(vec.begin(), vec.end(),
[threshold](const std::string& s) {
return s.length() < threshold;
}),
vec.end());
};
remove_short(lines);

std::vector<std::string> replaced = lines;
std::for_each(replaced.begin(), replaced.end(), [](std::string& s) {
std::replace(s.begin(), s.end(), ' ', '_');
});

std::string found_line = "Not found";
auto find_word = [&search_word,
&found_line](const std::vector<std::string>& vec) {
auto it = std::find_if(vec.begin(), vec.end(),
[&search_word](const std::string& s) {
return s.find(search_word) != std::string::npos;
});
if (it != vec.end()) {
found_line = *it;
}
};
find_word(replaced);

size_t total_no_underscore = 0;
for (const auto& s : replaced) {
for (char c : s) {
if (c != '_') total_no_underscore++;
}
}

std::vector<int> lengths;
std::transform(
replaced.begin(), replaced.end(), std::back_inserter(lengths),
[](const std::string& s) { return static_cast<int>(s.length()); });

std::ofstream output("output.txt");
if (!output) {
std::cerr << "Failed to create output.txt\n";
return 1;
}

output << "Original vector:\n";
for (const auto& s : original) output << s << "\n";
output << "\n";

output << "After removing strings shorter than " << threshold << ":\n";
for (const auto& s : lines) output << s << "\n";
output << "\n";

output << "After replacing spaces with '_':\n";
for (const auto& s : replaced) output << s << "\n";
output << "\n";

output << "Search result for \"" << search_word << "\": " << found_line
<< "\n\n";

output << "Total characters without '_': " << total_no_underscore << "\n\n";

output << "Vector of line lengths: ";
for (int len : lengths) output << len << " ";
output << "\n";

output.close();
std::cout << "Done. Check output.txt\n";

return 0;
}
15 changes: 15 additions & 0 deletions sem2/BelyaevNA/homework_3/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Player controller script
NPC AI behavior tree
HP bar UI element
Respawn point manager
Quest system update
Boss fight arena trigger
Low poly tree model
Inventory slot hover effect
Main menu background music loop
Save game data serializer
FOV slider settings
Critical hit chance calculator
Ragdoll physics test
Loot table randomizer
Enemy spawn wave logic
34 changes: 34 additions & 0 deletions sem2/BelyaevNA/homework_3/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Original vector:
Player controller script
NPC AI behavior tree
HP bar UI element
Respawn point manager
Quest system update
Boss fight arena trigger
Low poly tree model
Inventory slot hover effect
Main menu background music loop
Save game data serializer
FOV slider settings
Critical hit chance calculator
Ragdoll physics test
Loot table randomizer
Enemy spawn wave logic

After removing strings shorter than 25:
Inventory slot hover effect
Main menu background music loop
Save game data serializer
Critical hit chance calculator

After replacing spaces with '_':
Inventory_slot_hover_effect
Main_menu_background_music_loop
Save_game_data_serializer
Critical_hit_chance_calculator

Search result for "spawn": Not found

Total characters without '_': 100

Vector of line lengths: 27 31 25 30