You forgot to deal with the newline, which fgets also reads in if there is room in the buffer. Hence you get something like this:
Code:
Input: aaa bbb ccc...
Arg 1: aaa...
Arg 2: bbb...
Arg 3: ccc'\n'
...
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
char input[100];
char *p;
// Do some error checking on the call to fgets
if (fgets(input, 100, stdin) == NULL) {
perror("fgets");
exit(EXIT_FAILURE);
}
// Check if there is a newline in the buffer. If there is, get rid of it.
if ((p = strchr(input, '\n')) != NULL) {
*p = '\0';
}
char args[3][15];
int a = 0;
int i = 0;
int index = 0;
while (input[a] != '\0') {
if (input[a] == ' ') {
args[index][i] = '\0';
i = 0;
a++;
index++;
} // SPACE detected
args[index][i++] = input[a++];
printf("%c.", args[index][i - 1]);
}
args[index][i] = '\0';
printf("\nInput: %s...\n", input);
printf("Arg 1: %s...\n", args[0]);
printf("Arg 2: %s...\n", args[1]);
printf("Arg 3: %s...\n", args[2]);
return 0;
}
Now of course I cheated, because I am allowed to use standard functions. . I am confident that you will be able to adjust your code to deal with the newline without using them though.