给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出。
输入格式:测试输入包含一个测试用例,在一行内给出总长度不超过80的字符串。字符串由若干单词和若干空格组成,其中单词是由英文字母(大小写有区分)组成的字符串,单词之间用1个空格分开,输入保证句子末尾没有多余的空格。
输出格式:每个测试用例的输出占一行,输出倒序后的句子。
输入样例:Hello World Here I Come输出样例:
Come I Here World Hello
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char test[81] = {0};
gets(test);
int len = strlen(test);
char *s = test + len;
int count = 0;
while (1) {
if (s == test) {
printf("%s\n", s);
break;
}
if (*s == ' ' && *(s + 1) != ' ') {
*s = '\0';
printf("%s ", s + 1);
s--;
} else {
s--;
}
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
char test[81] = {0};
char ch;
char enter = 0xa; //回车
int i = 0; //字符串长度
int flag = 0; //空格标记
int count = 0; //单词数量
while (1) {
scanf("%c", &ch);
if (ch == enter) {
count++;
break;
}
if (ch == ' ') {
flag = 1;
} else {
if (flag == 1) {
test[i++] = ' ';
flag = 0;
count++;
}
test[i++] = ch;
}
}
int j = 0;
char **str = (char**)malloc(sizeof(char*) * count);
for (j = 0; j < count; j++) {
str[j] = (char*)malloc(sizeof(char) * i); //动态创建字符串数组
//memset(str[j], 0, sizeof(char) * i); //初始化字符串数组
}
int n = count;
char *begin = test;
while (count--) { //逆序写入字符串数组
for (j = 0; j < i; j++) {
if (*begin == ' ') {
begin++;
break;
}
if (*begin == '\0') {
break;
};
str[count][j] = *begin++; //写入字符串数组
}
}
for (i = 0; i < n; i++) {
if (i > 0) printf(" ");
printf("%s", str[i]);
}
printf("\n");
return 0;
}