欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Liunx系统下的C语言练习:把句子中的单词倒序操作,输入"i am from shanghai",输出"shanghai from am i"

程序员文章站 2022-04-03 17:13:17
Liunx系统下的C语言练习:把句子中的单词倒序操作,输入"i am from shanghai",输出"shanghai from am i&quo...

Liunx系统下的C语言练习:把句子中的单词倒序操作,输入"i am from shanghai",输出"shanghai from am i"

#include
#include
#include

int main()
{
	int count = 0;
	int i;
	int length;
	char *sen = (char *)malloc(sizeof(char) * 100);
	char *sen1 = (char *)malloc(sizeof(char) * 100);
	char *p = sen1;
	if(NULL == sen || NULL == sen1)
	{
		printf("NULL error\n");
		exit(1);
	}
	printf("请输入一句英文\n");
	gets(sen);
	while(1)
	{
		if(*sen == '\0' && *(sen + 1) == '\0') 
		break;
	 	*sen++;

	}
	sen --;
	//printf("%s\n", sen);
	while(1)
	{
		if(*sen =='\0' && *(sen - 1) == '\0')
		{
			break;
		}
		
		sen--;
		count ++;
		if(*sen ==' ' || *sen == '\0')
		{
			
		/*	if(*sen == '\0')
			{
				char *tmp = (char *)malloc(sizeof(char) * 20);
				if(NULL == tmp)
				{
					printf("NULL error");
					exit(1);
				}
				strncpy(tmp, sen + 1, count);
				printf("%s", tmp);
			}else
			{ */
				strncpy(sen1, sen + 1, count);
				sen1 = p;
				printf("%s ", sen1);
				memset(sen1, 0, count);
				count = 0;
		//	}	
		}
		//printf("%s\t", sen);
	}
	printf("\n");
	return 0;
}