Im trying to do some string parsing In C for homework but I just don't understand how to copy characters into a string...

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]); } ... }}

Other urls found in this thread:

pastebin.com/VQp0Gch3
pastebin.com/Gu6i7EcK
tutorialspoint.com/cprogramming/c_strings.htm
twitter.com/SFWRedditImages

Stop typing like a chav doing a drug deal via a featurephone's SMS and I might consider answering

sorry how would you like me to type?

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.

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.

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; } }}

It says right there that you're putting a non const in an function that wants a const.

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.

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*.

This works Thanks.

int main(){

char* test = "test this gay";
char newchar[strlen(test)];
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;
}
}
}

nvm my code is just calling me gay
int main(){ char* test = "test this gay"; char newchar[strlen(test)]; 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; } }}

#include #include #include "ChessMoves.h" #include #include #define ROW 8 #define COLUMN 8 int main(){ char* test = "test this gay"; char whitemove[strlen(test)]; char blackmove[strlen(test)]; 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(whitemove, &test[nc]); } if(test[nc] == ' ' && copy_flag == 0 && loop != 1){ printf("flag is on\n"); copy_flag = 1; loop = 1; } } printf("%s\n", whitemove); }

Shouldn't two of those if's be else if's? I don't know, I only glanced at it.

Also you cannot put a function between []. It has to be a constant. If you want it variable you will have to use malloc.

I changed that to just [100] but when i printf("%s", whitemoves);

it prints out This gayhis gays gay

and i dont get why its doing this.

pastebin.com/VQp0Gch3

There, it's sort of fixed. It's hack job, but it works. I'll explain it once you verify to me that it works.

Latest version of my code.
#include #include #include "ChessMoves.h" #include #include #define ROW 8 #define COLUMN 8 int main(){ char* test = "test this gay"; char* whitemove = malloc(sizeof(char ) * strlen(test)); char blackmove[strlen(test)]; 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){ strcat( whitemove , &test[nc] ); printf("%c\n", test[nc]); } if(test[nc] == ' ' && copy_flag == 0 && loop != 1){ printf("flag is on\n"); copy_flag = 1; loop = 1; } } printf("%s\n", whitemove); }

This is page 36-ish in k&r, OP. Your code sucks, go get an eBook of The C Programming Language

yes it works how did you do it.

Give me one moment, I'm adding comments to explain it, as well as cleaning it up.

bless user im reading through now.

I see the k++ is going through and adding the new char and then adding 1 and putting a EOF at the end. but how do you get rid of the end of files.,

...

(checked)

pastebin.com/Gu6i7EcK

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.

\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.

Not sure if this will help you understand it, but I Googled quickly and found this: tutorialspoint.com/cprogramming/c_strings.htm

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.

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.

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.

strncpy.

Dude, just use regex
` ([^ ]*)`
That's all, no need to write C

He has to since his homework required him too.