\0 in c++

#include <iostream>

int main() {
    char str[] = "Hello, World!";

    std::cout << "Original string: " << str << std::endl;

    str[5] = '\0';

    std::cout << "Modified string: " << str << std::endl;

    return 0;
}

Explanation:

  1. #include <iostream>: Includes the necessary library for input and output operations.

  2. int main() { ... }: The main function where the program execution begins.

  3. char str[] = "Hello, World!";: Declares and initializes a character array named str containing the string "Hello, World!".

  4. std::cout << "Original string: " << str << std::endl;: Outputs the original string stored in the variable str.

  5. str[5] = '\0';: Assigns the null character ('\0') to the character at index 5 in the str array, effectively truncating the string at that point.

  6. std::cout << "Modified string: " << str << std::endl;: Outputs the modified string after setting the character at index 5 to '\0'.

  7. return 0;: Indicates successful termination of the main() function.