\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:
#include <iostream>
: Includes the necessary library for input and output operations.int main() { ... }
: The main function where the program execution begins.char str[] = "Hello, World!";
: Declares and initializes a character array namedstr
containing the string "Hello, World!".std::cout << "Original string: " << str << std::endl;
: Outputs the original string stored in the variablestr
.str[5] = '\0';
: Assigns the null character ('\0'
) to the character at index 5 in thestr
array, effectively truncating the string at that point.std::cout << "Modified string: " << str << std::endl;
: Outputs the modified string after setting the character at index 5 to'\0'
.return 0;
: Indicates successful termination of themain()
function.