You understand that strings are basically an array with chars, with the last element in the array being '\0', right? The '\0' means "this is the end of the string." So we can abuse that to "truncate" the string. By doing that, all functions will think the string ends there, so we can easily copy just a part of the string using strcpy.
We can also use pointers to specify a start. Since a pointer can point anywhere in memory, we can just set it to a position within the string. When you pass that pointer to functions, they'll think the string starts where that pointer points (inside the string). Makes sense?
Therefore, we can do something like this:
Code:
char mystr[] = "This is my test string";
char temp[50];
char* p = NULL;
while ( p = strstr(mystr, " ") ) /* Find the first space */
{
*p = '\0'; /* Truncate the string */
strcpy(temp, mystr); /* Copy part of word to new buffer */
printf("%s ", temp); /* Print temporary buffer plus a space */
p++; /* Advance the position in the string to the next character */
/* Rinse and repeat! */
}
This is a simple example.