Hey!

I'm writing a program and currently having trouble with getting a written line typed in by the user to be copied correctly into another array...

I'm using a for-loop to run through the original char-array, but if I type in "Hello" the program will give me this result:
Param chararray: hello
ello
llo
lo
o
(which is exactly the same as the "midl"-array, but not at all what it want >;o) Hehe...)

What is causing this, and how do I avoid it?

int main(int argc, char *argv[])

{

	int teller = 1;

	while(teller!=0){

		printf("ifish %d > ", teller);

		teller++;

		char *returnkode;

		returnkode = fgets(text, maxl, stdin);

		printf("DEBUG --> Du skrev: %s 	\n", text);

		int i;

		int j=0;

		int parateller=0;

		for(i=0; i < (strlen(text)); i++){

			printf("Midl: %s\n", midl);

			strcat((char *)&midl, &text[i]);

			if((j=(isspace(text[i]))) != 0){

				printf("blank!\n");

				param[parateller] = malloc(sizeof(midl));

				strcpy(param[parateller], midl); 

				printf("Parateller: %d\n", parateller);

				parateller++;


			}

		}
                  printf("Param chararray: %s\n", param[0]);
        }

}

What is causing this, and how do I avoid it?

This line is causing it

strcat((char *)&midl, &text[i]);

how to avoid it? I can't say because I don't know what you intend for it to do.

Hey, and thanks for looking into this! :)

What I want it to split up a typed line into words and save them all as links to copies in the array "param". Then I'm going to use these words later...

So what I want is to copy the word(s) the user types from the text array to the midl array without it adding the enter and additional letters as if it's doing a countdown ;oP Hehe...

I'm guessing my for-loop is wrong and wonder if there is another loop I should use to run through text[] or if there is another way to divide the written line into words using isspace()?

If I understand you right, you want to split the sentence "Hello World" into an array of two strings -- "Hello" and "World". Assuming variable midl is just a temporary buffer to hold the current word, you do NOT want to use strcat() to copy the ith character into it.

int n = 0;
		for(i=0; i < (strlen(text)); i++){

			printf("Midl: %s\n", midl);

			midl[n++] = text[i];

			if( isspace(text[i]) ){
                                      midl[n] = 0; // null-terminate the string

				param[parateller] = malloc(sizeof(midl));

				strcpy(param[parateller], midl); 

				printf("Parateller: %d\n", parateller);
                                      n = 0;
				parateller++;


			}

That should handle the words before end-of-string is found, but not the last word. You need another check after that loop terminates so that you pick up the last word in the line.

I assumed I _had to_ use strcat or strcpy since I'm working with strings, but of course midl[n++] = text; is the answer!

Now I can finally try to do something with these words ;o) Thank you so much for your help!

>>param[parateller] = malloc(sizeof(midl));

The above is also wasteful -- should allocate the length of the string + 1

param[parateller] = malloc(strlen(midl)+1);

Why is adding +1 better than not having it there?

*noob who needs an explanation to everything* ;o) Hehe...

all c strings are terminated by 0. strlen() only returns the number of characters in the string so you have to add 1 more for the null terminator. If you don't very nasty things tend to happen to programs (been there, done that!)

This question has already been answered. Start a new discussion instead.