您的位置:首页 > 大数据 > 人工智能

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

2018-03-13 00:48 483 查看
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐