Sunday 15 September 2013

"%s ", string not printing space after string

"%s ", string not printing space after string

In this code snippet here:
printf("shell> ");
fgets(input, MAX_INPUT_SIZE, stdin);
//tokenize input string, put each token into an array
char *space;
space = strtok(input, " ");
tokens[0] = space;
int i = 1;
while (space != NULL) {
space = strtok(NULL, " ");
tokens[i] = space;
++i;
}
//copy tokens after first one into string
strcpy((char*)cmdargs, ("%s ",tokens[1]));
for (i = 2; tokens[i] != NULL; i++) {
strcat((char*)cmdargs, ("%s ", tokens[i]));
}
printf((char*)cmdargs);
With the input: echo hello world and stuff, the program prints:
helloworldandstuff
It seems to me that the line strcat((char*)cmdargs, ("%s ", tokens[i]));
should concatenate the string at tokens[i] with a space following it. Does
strcat not work with string formatting? Any other ideas what might be
going on?

No comments:

Post a Comment