std::string substr

#include <iostream>
#include <string>

int main() {
    std::string originalString = "Hello, World!";

    // Using substr() to extract a substring starting from index 7
    std::string extractedSubstring = originalString.substr(7);

    // Displaying the original string and the extracted substring
    std::cout << "Original String: " << originalString << std::endl;
    std::cout << "Extracted Substring: " << extractedSubstring << std::endl;

    return 0;
}
  1. Include necessary header files:
  2. #include <iostream>: Includes the input/output stream functions.
  3. #include <string>: Includes the C++ string library.

  4. Declare the main function:

  5. int main(): The main function where program execution begins.

  6. Create a string variable:

  7. std::string originalString = "Hello, World!";: Declares a string variable named originalString and initializes it with the value "Hello, World!".

  8. Use substr() to extract a substring:

  9. std::string extractedSubstring = originalString.substr(7);: Uses the substr() function to extract a substring from originalString starting at index 7 (i.e., the eighth character).

  10. Display the original string and extracted substring:

  11. std::cout << "Original String: " << originalString << std::endl;: Outputs the original string to the console.
  12. std::cout << "Extracted Substring: " << extractedSubstring << std::endl;: Outputs the extracted substring to the console.

  13. Return 0 to indicate successful execution:

  14. return 0;: Indicates that the program has executed successfully.