阅读背景:

Liunx_C练习:把句子中的单词倒序;(输入"i am from shanghai",输出"shanghai from am i")

来源:互联网 
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

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 == '
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

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;
}


' && *(sen + 1) == '
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

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;
}


') break; *sen++; } sen --; //printf("%s\n", sen); while(1) { if(*sen =='
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

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;
}


' && *(sen - 1) == '
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

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;
}


') { break; } sen--; count ++; if(*sen ==' ' || *sen == '
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

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;
}


') { /* if(*sen == '
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

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;
}


') { 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; } #include<stdio.h> #include<stdlib.h> #include<s



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: