在让用户输入他们的名字和姓氏后,我需要该程序执行多项工作,除了我需要编程以相反的顺序打印他们的名字(John Doe = Doe John)。由于我从你们那里得到了帮助,我认为我在它们中具有适当的功能,但是我仍然遇到分段错误。这里有什么问题。
这是程序中的最后一个函数
#include <stdio.h>
#include <string.h>
int main ()
{
printf("Enter your first and last name\n");
char name [25]={'\0'};
char * space;
fgets(name,sizeof(name),stdin);
printf("You Entered: %s \n", name);
printf("There are %u characters in your name including the space. \n", strlen(name));
char end;
int i;
end = strlen(name) -1;
printf("Your name backwards is");
for (i = end; i >= 0; --i)
{
printf("%c", name [i]);
}
printf("\nLooking for the space in your name \n", name);
space=strchr(name, ' ');
while (space!=NULL)
{
printf("The space was found at character %d\n", space-name+1);
space=strchr(space+1, ' ');
}
//Why am I getting a segmentation fault (cord dumped) error here?
*space = '\0';
printf(" %s %s ", space+1, name);
}