From 9583972dfd59ad34dfb8357330d4c7bfa860e64a Mon Sep 17 00:00:00 2001 From: pajjilykk Date: Wed, 1 Oct 2025 18:24:17 +0700 Subject: [PATCH] move shii --- lab_1/12_first_symbols_to_spaces.cpp | 75 ++++++++++++++++++++++++++-- lab_1/compile.sh | 1 + lab_1/file.in | 8 +++ lab_1/run.sh | 1 + 4 files changed, 82 insertions(+), 3 deletions(-) create mode 100755 lab_1/compile.sh create mode 100644 lab_1/file.in create mode 100755 lab_1/run.sh diff --git a/lab_1/12_first_symbols_to_spaces.cpp b/lab_1/12_first_symbols_to_spaces.cpp index 88d0a63..c8f2efb 100644 --- a/lab_1/12_first_symbols_to_spaces.cpp +++ b/lab_1/12_first_symbols_to_spaces.cpp @@ -1,3 +1,72 @@ -// -// Created by pajjilykk on 10/1/25. -// \ No newline at end of file +// Task 12 (Ubuntu): replace every char equal to the first char of its line with a space. +// Usage: ./task12 → writes ".out" and prints replacement count. + +#include +#include +#include +#include + +// True if file exists (POSIX stat) +static bool file_exists(const std::string &path) { + struct stat st{}; + return ::stat(path.c_str(), &st) == 0; +} + +// ".out" by replacing last extension or appending if none +static std::string make_out_name(const std::string &in) { + std::size_t pos = in.find_last_of('.'); + if (pos == std::string::npos) return in + ".out"; + return in.substr(0, pos) + ".out"; +} + +int main(int argc, char *argv[]) { + if (argc != 2) { + std::cerr << "Usage: " << argv[0] << " \n"; + return -1; + } + + std::string in_path = argv[1]; + if (!file_exists(in_path)) { + std::cerr << "Error: input file does not exist.\n"; + return -1; + } + + std::ifstream in(in_path); + if (!in) { + std::cerr << "Error: cannot open input file.\n"; + return -1; + } + + std::string out_path = make_out_name(in_path); + std::ofstream out(out_path); + if (!out) { + std::cerr << "Error: cannot create output file.\n"; + return -1; + } + + long long replacements = 0; + std::string line; + + while (std::getline(in, line)) { + if (!line.empty()) { + char first = line.front(); + for (char &c: line) { + if (c == first) { + if (c != ' ') { + c = ' '; + ++replacements; + } // count only real changes + } + } + } + out << line; + if (!in.eof()) out << '\n'; + if (!out) { + std::cerr << "Error: write error.\n"; + return -1; + } + } + + std::cout << replacements << std::endl; + return static_cast(replacements); +} diff --git a/lab_1/compile.sh b/lab_1/compile.sh new file mode 100755 index 0000000..e5febee --- /dev/null +++ b/lab_1/compile.sh @@ -0,0 +1 @@ +g++ -std=c++17 -O2 -Wall -Wextra 12_first_symbols_to_spaces.cpp -o 12_first_symbols_to_spaces diff --git a/lab_1/file.in b/lab_1/file.in new file mode 100644 index 0000000..11e1de9 --- /dev/null +++ b/lab_1/file.in @@ -0,0 +1,8 @@ +abcabcABC +Aaaaaa +1112233111 +,,,,.,, +leading spaces here +tab leading here +first-letter f repeats: f fff f-f_f +ZzzZzZ \ No newline at end of file diff --git a/lab_1/run.sh b/lab_1/run.sh new file mode 100755 index 0000000..8d3156f --- /dev/null +++ b/lab_1/run.sh @@ -0,0 +1 @@ +./12_first_symbols_to_spaces file.in