#include#include int main() { char str[] = "Hello World!"; std::string myStr(str); // create string object from c-style string std::cout << myStr << std::endl; return 0; }
#include#include int main() { std::string myStr = "Hello World!"; for (int i = 0; i < myStr.length(); i++) { char c = myStr[i]; // get the i-th character of the string std::cout << c << " has ASCII value " << (int)c << std::endl; // print the ASCII value of the character } return 0; }
#includeIn this example, we convert a C++ string object "Hello World!" to its ASCII encoding by looping through each character and printing its ASCII value. Package/Library: Standard Library#include int main() { std::string myStr = "Hello World!"; for (int i = 0; i < myStr.length(); i++) { char c = myStr[i]; // get the i-th character of the string int asciiVal = (int)c; // get the ASCII value of the character std::cout << asciiVal << " "; // print the ASCII value } std::cout << std::endl; return 0; }