C语言作业3-数组-2英文句子逆向输出
问题描述:
把一个英文句子中的前后单词逆置 (单词之间用空格隔开)
如: how old are you
逆置后为: you are old how?
代码实现:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 100
int main()
{
char a[MAX];
int n,//长度
i,
j;
printf("请输入需要逆向输出的句子:\n");
fgets(a,MAX,stdin);
n = strlen(a);
for(i = n - 2;i > 0;i--)
{
if(a[i] == 32)
{
j = i + 1;
while (a[j] != 32)
{
if(j == n-1)
break;
printf("%c",a[j]);
j++;
}
printf(" ");//打印句子单词间空格
}
}
while(a[i]!=32)//打印原字符串中第一个单词
{
printf("%c",a[i]);
i++;
}
return 0;
}
运行结果:
欢迎各位大佬提出更优秀的解决方案,小白若有疑惑欢迎评论提问。