0

Trying to use strtok() to seperate words in input

Hi there, I'm trying to get strtok() to take a 50 character input and then display the words seperated by a space. For example if I input hello1 hello2 hello3. Can someone please advise where I'm wrong and why from a learning perspective. https://code.sololearn.com/cUyC49BF0mRg/?ref=app https://code.sololearn.com/cUyC49BF0mRg/?ref=app

12th Apr 2020, 5:35 AM
Matt
Matt - avatar
24 Answers
0
Hi. Read this for better scanf understanding https://www.tutorialspoint.com/c_standard_library/c_function_scanf.htm You should provide another format, not "%s". Also you don't use strtok properly. Example of strtok usage https://www.geeksforgeeks.org/strtok-strtok_r-functions-c-examples/
12th Apr 2020, 6:10 AM
Stephan
Stephan - avatar
0
Try to correct solution for yourself. If you are completely stuck here is my solution. https://code.sololearn.com/cJ9nPA27PzuZ/?ref=app
12th Apr 2020, 6:11 AM
Stephan
Stephan - avatar
0
Thanks it seems I'm missing // Keep printing tokens while one of the // delimiters present in str[]. while (token != NULL) { printf("%s\n", token); token = strtok(NULL, "-"); }
12th Apr 2020, 6:18 AM
Matt
Matt - avatar
0
You' re right. Also either use another delimeter instead of spaces or change scanf format
12th Apr 2020, 6:21 AM
Stephan
Stephan - avatar
0
char array[50], input; scanf("%s",&input); char * token = strtok(input, " " ); while (token){ printf("%s\n", token); token = strtok(NULL, " "); return 0; } Hi Stephan, I know you suggested to remove %s, however I want to understand why I get error "Makes pointer from interger without a cast" What does this mean?
12th Apr 2020, 8:33 AM
Matt
Matt - avatar
0
Do you know how C stores arrays in memory?
12th Apr 2020, 8:35 AM
Stephan
Stephan - avatar
0
Not particularly, I've read the below to gain a better understanding. Makes pointer from interger without cast ? I understand a pointer is a variable with a memory address, I'm assuming each element in array has got a memory address? int* p; // variable p is pointer to integer type Im assuming in my program it's putting pointer addresses for each " " (space) in array."token" is the variable for these spaces ? was does this without cast referring to int i; // integer value int i2 = *p; // integer i2 is assigned https://www.cs.bu.edu/teaching/cpp/string/array-vs-ptr/
12th Apr 2020, 9:12 AM
Matt
Matt - avatar
0
Oh. I missed this comma in the first line of your code. Remove it.
12th Apr 2020, 9:23 AM
Stephan
Stephan - avatar
0
But that doesn't make sense. Both pointer and array variable store address in memory. Just one address of the first element. Try to run this code https://code.sololearn.com/c7DmU3vQgFtH/?ref=app
12th Apr 2020, 9:29 AM
Stephan
Stephan - avatar
0
Code has errors, so I changed it
12th Apr 2020, 9:35 AM
Stephan
Stephan - avatar
0
So as you can see array and pointer variables actually are numbers that represent memory address. After that you should understand & operator. This operator gives you address where variable is stored. Suppose we declared char a; &a gives you addres of value stored in variable. So you can do something like this char *p = &a;
12th Apr 2020, 9:43 AM
Stephan
Stephan - avatar
0
Ran yours, it worked however it still warned "makes pointer from interger without a cast" I'm trying to understand this error. Google says: A char is a form of integer in C. You are assigning it into a char[] which is a pointer. Hence "converting integer to pointer".
12th Apr 2020, 9:45 AM
Matt
Matt - avatar
0
I had different code in my mind.
12th Apr 2020, 9:54 AM
Stephan
Stephan - avatar
0
At first you should add [50] in input declaration and add closing bracket
12th Apr 2020, 9:55 AM
Stephan
Stephan - avatar
0
Code should run, but with some warnings
12th Apr 2020, 10:02 AM
Stephan
Stephan - avatar
0
char input; - input is single character char input[50]; - input is memory address
12th Apr 2020, 10:16 AM
Stephan
Stephan - avatar
0
Ok thanks. int main() {     char str[] = "Geeks-for-Geeks";        // Returns first token     char* token = strtok(str, "-");        // Keep printing tokens while one of the     // delimiters present in str[].     while (token != NULL) {         printf("%s\n", token);         token = strtok(NULL, "-");     }     return 0; I think I understand the above now basically a pointer variable is set for each "-" in array elements...which is marked NULL. And while token does not equal NULL print each element in array
12th Apr 2020, 10:22 AM
Matt
Matt - avatar
0
Hi Stephan, back again. How do you compare a variable, which I'm assuming in "token" is a ptr address to a literal string. Should I be using a function such as strcmp? https://code.sololearn.com/cy0vNDXAjjKD/?ref=app
14th Apr 2020, 10:13 AM
Matt
Matt - avatar
0
Hi. You should. Pay attention at return value of strcmp, it becomes zero when strings are equal. Also you should remove ampersand symbols.
14th Apr 2020, 10:35 AM
Stephan
Stephan - avatar
0
By the way. When you compare like this token == "some text" you actually compare adresses, not values ("some text" has type char[10]).
14th Apr 2020, 10:39 AM
Stephan
Stephan - avatar