// I added this line in. Some of the functions used by this program are old and unsafe,
// so Visual Studio won't let you use them unless this is at the top of the file.
#define _CRT_SECURE_NO_WARNINGS
// This tells your compiler to include the file named "iostream".
// It is needed for this program to use the function "std::cout <<".
#include <iostream>
// This tells your compiler to include the file named "cstring".
// It is needed for this program to use the functions "strcpy()" and "strtok()".
// It also allows you to easily print C-Strings to the console.
#include <cstring>
// This tells your compiler to include the file named "string".
// It is needed for this program to create the object "str", and to use the functions ".length()" and ".c_string()".
#include <string>
// This declares the start of the main function.
// Every C++ program must have a function called main, and it must be of type int.
// Everything within the following bracket and its accompanying bracket at the end are within the function "main()".
int main ( )
{
// This creates a string object called "str" containing the contents within the quotation marks.
std::string str ( "Please split this sentence into tokens" );
// This creates a char pointer called "cstr".
// It then allocates enough ram to hold a new char array of size "str.length()+1".
// The function "str.length()" returns the length of the string object "str".
// C-Strings must always end with a null character, which is why it is one character longer than "str".
// The null character is placed automatically.
char * cstr = newchar [str.length()+1];
// This copies the contents of "str" into the space allocated for "cstr".
// The function ".c_str()" takes a string object and returns a pointer to a char array.
std::strcpy ( cstr, str.c_str ( ) );
// This creates a char pointer called "p".
// It then points it at the begining of "cstr" and replaces the first space it finds with a null character.
// This function should really not be used since it does this.
char * p = std::strtok ( cstr, " " );
// This is a loop. Everything within the brackets will be looped until "p != 0" is false (when p == 0).
// "while ( p )" would do the same thing.
while ( p != 0 )
{
// This will print the C-String "p" to the console,
// followed by the new line character to move the output down one line.
std::cout << p << '\n';
// This does the same things as previously with one difference.
// When this function was called previously, it saved the location of the character
// right after the null characteer it added. By calling this function with "NULL"
// instead of "cstr" it will start at that location instead of at the start of "cstr".
p = std::strtok ( NULL, " " );
}
// This deallocates the memory previously allocated for "cstr".
// If you use "new" to allocate memory, you must use "delete" to deallocate it.
delete[] cstr;
// This tells the function main to return a value of 0, ending the program.
// Any other return value would tell your computer that this program encountered an error.
return 0;
}