
Другие языки программирования и технологии
3 задачи по C++ про строки.
8,9,10


#include <iostream>
#include <string>
#include <regex>
#include <algorithm>
#include <sstream>
using namespace std;
class Task {
public:
static string remove_punct(const string& line) {
return regex_replace(line, punct, "");
}
static bool palindrome(const string& line) {
auto str = line;
transform(str.begin(), str.end(), str.begin(), tolower);
return str == string(str.rbegin(), str.rend());
}
static string trim(const string& line) {
auto str = regex_replace(line, regex("\\s+"), " ");
str.erase(0, str.find_first_not_of(" "));
str.erase(str.find_last_not_of(" ") + 1);
return str;
}
static size_t max_length(const string& line) {
istringstream iss(line);
string word;
size_t length = 0;
while (iss >> word) {
word = remove_punct(word);
auto n = word.length();
if (n > length) length = n;
}
return length;
}
private:
static const regex punct;
};
const regex Task::punct("[,.;:!?]+");
void puts(string_view str) {
cout << str << '\n';
}
string get_line(const char* msg = "String: ") {
cout << msg;
string line;
getline(cin, line);
return line;
}
void task_1(const char* msg) {
puts(msg);
puts(Task::remove_punct(get_line()));
}
void task_2(const char* msg) {
puts(msg);
Task::palindrome(get_line()) ? puts("Yes!") : puts("No!");
}
void task_3(const char* msg) {
puts(msg);
puts(Task::trim(get_line()));
}
void task_4(const char* msg) {
puts(msg);
puts(to_string(Task::max_length(get_line())));
}
int main() {
task_1("\t Task 1. Remove punctuation");
task_2("\t Task 2. Check for palindrome");
task_3("\t Task 3. Remove extra spaces");
task_4("\t Task 4. Longest word");
system("pause");
}
#include <string>
#include <regex>
#include <algorithm>
#include <sstream>
using namespace std;
class Task {
public:
static string remove_punct(const string& line) {
return regex_replace(line, punct, "");
}
static bool palindrome(const string& line) {
auto str = line;
transform(str.begin(), str.end(), str.begin(), tolower);
return str == string(str.rbegin(), str.rend());
}
static string trim(const string& line) {
auto str = regex_replace(line, regex("\\s+"), " ");
str.erase(0, str.find_first_not_of(" "));
str.erase(str.find_last_not_of(" ") + 1);
return str;
}
static size_t max_length(const string& line) {
istringstream iss(line);
string word;
size_t length = 0;
while (iss >> word) {
word = remove_punct(word);
auto n = word.length();
if (n > length) length = n;
}
return length;
}
private:
static const regex punct;
};
const regex Task::punct("[,.;:!?]+");
void puts(string_view str) {
cout << str << '\n';
}
string get_line(const char* msg = "String: ") {
cout << msg;
string line;
getline(cin, line);
return line;
}
void task_1(const char* msg) {
puts(msg);
puts(Task::remove_punct(get_line()));
}
void task_2(const char* msg) {
puts(msg);
Task::palindrome(get_line()) ? puts("Yes!") : puts("No!");
}
void task_3(const char* msg) {
puts(msg);
puts(Task::trim(get_line()));
}
void task_4(const char* msg) {
puts(msg);
puts(to_string(Task::max_length(get_line())));
}
int main() {
task_1("\t Task 1. Remove punctuation");
task_2("\t Task 2. Check for palindrome");
task_3("\t Task 3. Remove extra spaces");
task_4("\t Task 4. Longest word");
system("pause");
}
Похожие вопросы
- Помогите написать задачу по C++ Дана строка. найти в ней самое длинное слово-палиндром.
- Помогите решить задачи на C#
- Напишите задачу на C++. Найти, какая оценка наиболее часто встречалась на ЕГЭ. Входные данные: 1 1 1 2 2 3 1 3 4 5 3.
- Помогите решить задачу на C или C++, пожалуйста.
- Помогите решить задачу по C++
- Помогите с решением задачи на C++
- Помогите решить задачи по C++
- C передать строку 1 параметра в массив
- VS/C++ разбиение строки из 10 слов.
- c++, массивы, строки