hi
i have this problem i read lines from a txt but i want to store each word into a char*.i use fgets for the line but i cant think of a way to separate the words..
hi
i have this problem i read lines from a txt but i want to store each word into a char*.i use fgets for the line but i cant think of a way to separate the words..
Your first task is to think of characters that separate words.
Your second task is to write code to find those characters in what fgets() returns.
Your third task is to use that information to copy all the substrings into an array of substrings.
And you're done.
Or you can just use strtok() and you can use the rest of the time you just saved making yourself a nice cup of tea
1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
3. Get rid of conio.h and other antiquated DOS crap headers.
4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.
Your first step would be to look up strtok() in your compiler's library documentation. You might also want to look at scanf() while you're at it.
Neither are especially hard to use but they're not the world's simplest functions either.
So work it out on paper, following the description, first. Then when you think you understand it, tap in some trial code and see what happens.
Finally, if you get stuck --real stuck, not lazy stuck-- post up your code and we'll see what we can do to help.