给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出。
输入格式:
测试输入包含一个测试用例,在一行内给出总长度不超过 80 的字符串。字符串由若干单词和若干空格组成,其中单词是由英文字母(大小写有区分)组成的字符串,单词之间用 1 个空格分开,输入保证句子末尾没有多余的空格。
输出格式:
每个测试用例的输出占一行,输出倒序后的句子。
#include <stdio.h>
#include <string.h>
int main(){
int i, len;
char str[81]; //字符串长度不超过80
gets(str); //读取字符串
len = strlen(str); //字符串长度函数
for (i=len;i>=1;i--){
if(str[i-1]==' '){
printf("%s ", &str[i]);
str[i-1]='\0';
}
}
printf("%s\n", str);
}
要点:
The strlen() function calculates the length of a given string.
It is defined in the <string.h> header file.
Note that the strlen()
function doesn't count the null character \0
while calculating the length.