Is there a function in c where I can copy specific parts of a char array
Is there a function in c where I can copy specific parts of a char array
I have the char
char test = "Hello theren"
"World, my namen"
"Is bobn";
Is there a function where I can use to store each line into a different char array. So the n
would indicate for me to stop, and store it into a new array? So in the end I would have 3 sets of char arrays. Essentially is there a function which I can use to find the n
, then copy everything before it, but then stop copying if it sees a n
n
n
n
char line1 = "Hello theren";
char line2 = "World, my namen";
char line3 = "Is bobn";
1 Answer
1
You can use strtok as seen in the example below.
#include<string.h>
#include<stdio.h>
int main()
char test = "Hello theren"
"World, my namen"
"Is bobn";
char *p;
p = strtok(test, "n");
while(p)
printf("%sn", p);
p = strtok(NULL, "n");
return 0;
n
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
How can I also ensure that
n
also gets added into each line as well.– Sarah Chan
Sep 9 '18 at 9:43