Другие языки программирования и технологии

Помогите написать программу на C++. Удалить из текста символы «_» и подсчитать длину сформированного текста

Задание на тему: Строки. Обработка символьных данных
#include <iostream>
#include <string>
#include <regex>
using namespace std;
int main() {
string line = "__abc_def__ghi_jkl_";
cout << line << " : length = " << line.length() << '\n';
line = regex_replace(line, regex("_"), "");
cout << line << " : length = " << line.length() << '\n';
system("pause");
}
Ян Сергеев
Ян Сергеев
96 219
Лучший ответ
Сакен Алшинбаев Чем метод length() отличается от size() для класса string?
#include <iostream>
#include <windows.h>
#include <fstream>
#include <string>
#include <algorithm>
#include <vector>

using namespace std;

int main()
{
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
system("color 0A");

string addr;
vector<string> text;
cout << "Введите полное имя файла *.txt" << endl;
getline(cin, addr);
auto fin = ifstream(addr);
if (!fin.is_open())
{
cout << "Файл не удаётся открыть для чтения" << endl;
Sleep(3000u);
return -1;
}
for (;fin.good();)
{
string s;
getline(fin, s);
text.push_back(s);
}
fin.close();
auto predicate = [](char c)
{
return c == '_';
};
for (auto &str : text)
{
auto it_remove = remove_if(str.begin(), str.end(), predicate);
str.erase(it_remove, str.end());
}
auto fout = ofstream(addr);
if (!fout.is_open())
{
cout << "Файл не удаётся открыть для записи" << endl;
Sleep(3000u);
return -1;
}
size_t ind = 0u;
size_t len = 0u;
size_t count_strs = text.size();
for (const auto &str : text)
{
len += str.size();
fout << str << (ind++ == count_strs - 1u ? "" : "\n");
}
fout.close();
cout << "Длина обработанного текста = " << len << endl;

system("pause");
return 0;
}
Elaman Abdin
Elaman Abdin
8 552