std::string str1 = "Hello"; std::string str2 = ""; if (str1.empty()) { std::cout << "str1 is empty" << std::endl; } else { std::cout << "str1 is not empty" << std::endl; } if (str2.empty()) { std::cout << "str2 is empty" << std::endl; } else { std::cout << "str2 is not empty" << std::endl; }
str1 is not empty str2 is empty
std::string input; std::cout << "Enter a string: "; std::cin >> input; if (!input.empty()) { std::cout << "The string you entered is: " << input << std::endl; } else { std::cout << "You didn't enter anything!" << std::endl; }
Enter a string: Hello The string you entered is: HelloThe String IsEmpty function is a built-in function provided by the C++ standard library, which means it is included in the standard C++ library (including iostream and string headers).