This is more like how I'd present the code in the question — before I passed it through a compiler:
void order(char ar[])
{
int count = 0, a = 1;
char *token;
char last[20], middle[20], first[20];
char s[] = " ";
for (int i = 0; i < strlen(ar) - 1; i++)
{
if (ar[i] == ' ')
count++;
}
token = strtok(ar, s);
strcpy(last, token);
while (token != NULL)
{
token = strtok(NULL, s);
if (a < count)
strcpy(middle, token);
else
strcpy(first, token);
a++;
}
printf("%s %s %s", first, middle, last);
}
You can argue about brace placement — I use Allman but 1TBS is a widely used alternative style. I recommend choosing one of those two. I put braces around the body of a loop if it is more than one line; YMMV.
Use more white space around operators. Don't define unused variables (c
vanished). I placed int i
in the for
loop. I would probably define one variable per line, and I probably wouldn't use the count
or a
.
The call to strlen()
should not be in the condition of the loop. I'm not sure how you'd cope with my name — I don't have a middle name. I think you could avoid the while
loop altogether. I'm not convinced you need the for
loop either. Those are a separate discussion.
I'm not attempting to fix the algorithmic problem — this is not an answer to the question in the question (so it shouldn't be accepted), but is an answer to the question in the comment, and since code cannot be formatted in comments, I can't answer it sanely in a comment; hence, this 'not an answer' but 'intended to be helpful to an auxilliary question from the OP'.