Технологии
Решено
написать программу в С++, если Длина L 15, то удаляются все a..z; - вопрос №5318134
ноябрь 9, 2023 г.
-
Всего ответов: 3
-
если все правильно понял задачу, то:
С++
#include <iostream>
#include <algorithm>
int main() {
std::string str = «abc123def456ghi789»; // Ваша строка
str.erase(std::remove_if(str.begin(), str.end(), ::isalpha), str.end());
std::cout << str << std::endl;
return 0;
}
После выполнения этого кода в консоли будет выведено: «123456789», так как все буквы были удалены из строки. -
#include <iostream>
#include <fstream>
using namespace std;
class MyString {
public:
MyString() {
cout << «Конструктор по умолчанию» << endl;
length = 0;
str = nullptr;
}
MyString(const char* str) {
cout << «Конструктор с параметрами» << endl;
length = strlen(str);
this->str = new char[length + 1];
strcpy(this->str, str);
}
MyString(const MyString& other) {
cout << «Конструктор копирования» << endl;
length = other.length;
str = new char[length + 1];
strcpy(str, other.str);
}
~MyString() {
cout << «Деструктор» << endl;
if (str != nullptr) {
delete[] str;
}
}
void set(const char* str) {
cout << «Метод set» << endl;
length = strlen(str);
if (this->str != nullptr) {
delete[] this->str;
}
this->str = new char[length + 1];
strcpy(this->str, str);
}
void update() {
cout << «Метод update» << endl;
ofstream out(«output.txt»);
out << str << endl;
out.close();
ifstream in(«output.txt»);
in.getline(str, 100);
in.close();
}
void print() {
cout << «Метод print» << endl;
cout << str << endl;
}
private:
int length;
char* str;
};
int main() {
MyString str1;
str1.set(«Hello, world!»);
str1.print();
MyString str2(«Goodbye, world!»);
str2.print();
MyString str3(str2);
str3.print();
str1.update();
str1.print();
return 0;
}Лучший ответ по мнению автора -
С++
#include <iostream>
#include <cstring>
#include <fstream>
class MyString {
private:
char* str;
public:
// Конструктор без параметров
MyString() {
str = new char[1];
str[0] = '\0';
std::cout << «Конструктор без параметров вызван\n»;
}
// Конструктор с параметрами
MyString(const char* input) {
str = new char[strlen(input) + 1];
strcpy(str, input);
std::cout << «Конструктор с параметрами вызван\n»;
}
// Конструктор копирования
MyString(const MyString& other) {
str = new char[strlen(other.str) + 1];
strcpy(str, other.str);
std::cout << «Конструктор копирования вызван\n»;
}
// Деструктор
~MyString() {
delete[] str;
std::cout << «Деструктор вызван\n»;
}
// Метод ввода строки
void set(const char* input) {
delete[] str;
str = new char[strlen(input) + 1];
strcpy(str, input);
std::cout << «Метод set вызван\n»;
}
// Метод изменения строки
void update() {
if (strlen(str) > 15) {
for (int i = 0; i < strlen(str); i++) {
if (str[i] >= 'a' && str[i] <= 'z') {
str[i] = ' ';
}
}
}
std::cout << «Метод update вызван\n»;
}
// Метод вывода строки
void print() {
std::cout << str << "\n";
}
};
Теперь мы можем использовать этот класс в нашей программе:
С++
int main() {
MyString myStr(«Привет, мир! Это тестовая строка.»);
myStr.print(); // Выводит: «Привет, мир! Это тестовая строка.»
myStr.update();
myStr.print(); // Выводит: «Привет, мир! Это тестовая строка.» без маленьких букв a..z
return 0;
}
Похожие вопросы
составить программу в С++ Поезд прибывает на станцию в а часов b минут и отправляется в с часов d минут. Пассажир пришел на платформу в n часов m минут. Составить программу, определяющую: будет ли пое
май 11, 2015 г.
Написать программу, которая удаляет элемент из массива, если среднее арифметическое его "соседей" меньше 5
июнь 8, 2015 г.
Решено
Дан текстовый файл F. Переписать в другой файл G все строки, содержащие цифры.
февраль 16, 2012 г.