Im trying to do some string parsing In C for homework but I just don't understand how to copy characters into a string. I just want to loop through the Test string and when i get to a space i want to start copying over and when i get to the second space i want it to stop copying. I already got the start and stop flags to work I just need help on copying over the chars to the new string.
#include #include #include #include int main(){ char* test = "test this gay"; char *newchar; int i; int nc; int copy_flag = 0; for(nc = 0; nc < strlen(test); ++nc){ int loop = 0; ... if(copy_flag == 1){ printf("%c\n", test[nc]); strcat(newchar, test[nc]); } ... }}
Stop typing like a chav doing a drug deal via a featurephone's SMS and I might consider answering
Kayden Allen
sorry how would you like me to type?
Easton Williams
I'm pretty sure loop is going to be set to 0 at the start of every loop unless you move it outside the loop.
Grayson Bell
yes sorry that you cant see it in the code (I had to cut some things out)
but loop is set to 0 at the start to make sure that the flag doesn't turn on and off.
when its set to 0 the if statements can run but when its set to 1 the if statements wont run.
Cooper Anderson
int main(){ char* test = "test this gay"; char *newchar; int i; int nc; int copy_flag = 0; for(nc = 0; nc < strlen(test); ++nc){ int loop = 0; if(test[nc] == ' ' && copy_flag == 1 && loop != 1){ printf("flag is off\n"); copy_flag = 0; loop = 1; } if(copy_flag == 1){ printf("%c\n", test[nc]); strcat(newchar, test[nc]); } if(test[nc] == ' ' && copy_flag == 0 && loop != 1){ printf("flag is on\n"); copy_flag = 1; loop = 1; } }}
Jacob Ramirez
It says right there that you're putting a non const in an function that wants a const.
Julian Carter
It shits itself because you never allocate any memory for the new char, leaving newchar pointing to some random location, causing the segfault when you start writing to it.
Also, that warning it spits out about strcat is right. You want to do &test[nc] instead of test[nc] so you get the address (rather than content) of the ncth position.
Angel Watson
Const in a function argument just means the function wont modify it. You can pass non-const to const, but (not the other way around). It's bitching because it's getting a char instead of char*.
Jaxson Hall
This works Thanks.
int main(){
char* test = "test this gay"; char newchar[strlen(test)]; int i; int nc; int copy_flag = 0;
Here it is with comments and cleaned up. Mind you, you could take this one step farther and allocate only the memory you need by calculating the index the middle word starts at, where it ends, that gives you where it is, and how long it is, but this is simpler and I'm lazy.
Julian Brown
\0 isn't EOF, it's the null terminator. Basically tl;dr it's a way of hinting at the end of string, even if the data is shorter than the buffer. Not all functions require a string to have it, but printf does.
No I understand fully now thanks. the more I was trying to work with strcpy() the more everything was braking. but with your implementation i don't have to mess with it.
Oliver Scott
You can do it with strcpy, it would be better since it would be the proper way, but since you already had a loop, I decided just to work with that.
Angel White
Or wait, sorry you can't. Strcpy doesn't work the way I thought it did. Strange, I seriously thought strcpy had a length argument, but it doesn't.
Robert Gutierrez
strncpy.
Adam Myers
Dude, just use regex ` ([^ ]*)` That's all, no need to write C