Can you explain this line by line

im not good in c++ so please leave a comment in every line of this code thanks in advance.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  // strings and c-strings
#include <iostream>
#include <cstring>
#include <string>

int main ()
{
  std::string str ("Please split this sentence into tokens");

  char * cstr = new char [str.length()+1];
  std::strcpy (cstr, str.c_str());

  // cstr now contains a c-string copy of str

  char * p = std::strtok (cstr," ");
  while (p!=0)
  {
    std::cout << p << '\n';
    p = std::strtok(NULL," ");
  }

  delete[] cstr;
  return 0;
}
Last edited on
I added a few comments. ;)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// 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 = new char [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;
}
Last edited on
Topic archived. No new replies allowed.