1. I have a question in C++ about strings. For example I wrote this code:
Then the temp returns "sihJay" but I want it to return just "sih". I mean I want to empty temp before copying. after the codes above, I want to reverse the temp by strrev and there, the problem will occure! I have tried all these codes to empty temp:
Some would give Error and some would make no change to temp. I want you to show me the way to empty temp just like when it was declared at the begining.
2. is it possible to have strtok with more than one second parameter? I mean, for example you have a sentence like "Hello! Is this a test, you know?" and you want to separate the words of it by " ","!","?",",",etc. So how would you do that?
Thanks a lot
bm, Thank you for your questions.
1. Your code uses pointer variables "temp" and "token" incorrectly. A pointer variable, as the name implies, is simply a variable containing the address of a memory location. To use a pointer variable correctly, you need to do two things: allocate some memory for program use, and set the pointer variable to the address of the allocated memory. Here is how to do this:
// This declares a pointer variable:
char * pMyString;
// This declares a memory block (a fixed-length array of characters):
// This sets the pointer to point to the first character of the string:
pMyString = MyString;
// At this point you can store a string
// Or, you could avoid using the pointer entirely (pointers are not needed for simple operations):
// To empty the string, store a NUL character (\0) at offset 0 in the array:
*MyString = 0;
// This is equivalent:
MyString = 0;
2. "strtok" only takes two parameters. The second is a list of recognized delimiters; in your case, it might be "!,?". If you want to search first for one delimiter, then for another, you have to do two separate searches.