My problem is: Input name and print out in the order
Ex: input the name : Martin Luther King
result : King,Martin-Luther
But when i run this code it prints out : King,LutherMartin-Luther. Can you help show me the mistake?
#include<stdio.h>
#include<conio.h>
#include <string.h>
int main()
{
char name[100];
int i;
printf("input full name: "); gets(name);
char * c =&name[strlen(name)-1];
while (*(c)!=' ')
c--;
printf("%s,",c);
*c= '\0';
char *d=&name;
while (*(d)!=' ')
d++;
printf("%s",d);
*d='-';
printf("%s",name);
getch();
return 0;
}
*dto NULL, you set it to'-'. At that point, name becomes"Martin-Luther\0King\0". So the last printf prints "Martin-Luther". So, your second printf is unnecessary if you're converting "X Y Z" to "Z,X-Y".