-
ya
- ^-^
- Сообщения: 2675
- Зарегистрирован: 16 дек 2021, 19:56
Сообщение
ya »
проверит, что строка соответствует шаблону [A-Za-z0-9]
Код: Выделить всё
#include <iostream>
#include <string>
#include <regex>
bool isAlnumString(const std::string& str) {
// Регулярное выражение: вся строка состоит из букв и цифр
static const std::regex pattern("^[A-Za-z0-9]+$");
return std::regex_match(str, pattern);
}
-
ya
- ^-^
- Сообщения: 2675
- Зарегистрирован: 16 дек 2021, 19:56
Сообщение
ya »
проверит, что строка не содержит апострофа
Код: Выделить всё
#include <iostream>
#include <string>
bool doesNotContainApostrophe(const std::string& str) {
// Проверяем, есть ли апостроф в строке
return str.find('\'') == std::string::npos;
}
int main() {
std::string test1 = "Hello World!";
std::string test2 = "It's a test.";
std::cout << std::boolalpha;
std::cout << "Test 1 (без апострофов): " << doesNotContainApostrophe(test1) << std::endl; // true
std::cout << "Test 2 (с апострофом): " << doesNotContainApostrophe(test2) << std::endl; // false
}