翻转一个句子,保证单词顺序不变

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

char string[100] = {0};

typedef struct data{
	char content[10];
	int num;
	struct data *next;
}node;

void main(){
	int i, No = 0, k = 0;
	char temp[10]= {0};
	node *data, *head, headNode;

	head = &headNode;
	printf("请输入要翻转的字符串:\r\n");
	gets(string);
	for(i = 0 ; i <= (int)strlen(string); i++){
		if (string[i] != ' ' && string[i] != 0){
			temp[k] = string[i];
			k++;
		}
		else{
			data = (node *)malloc(sizeof(node));
			strcpy(data->content, temp);
			memset(temp, 0, sizeof(temp));
			data->num = No;
			k = 0;
			
			if(No == 0){
				head->next = data;
				data->next = NULL;
			}
			else{
				data->next = head->next;
				head->next = data;			
			}
			No++;			
		}
	}

	head = headNode.next;

	printf("\r\n");
	while(head != NULL){
		printf("%s ", head->content);
		data = head;
		head = head->next;
		free(data);
	}
	printf("\r\nright!!");
}



   

你可能感兴趣的:(翻转一个句子,保证单词顺序不变)