题解 | #单词倒排#
单词倒排
https://www.nowcoder.com/practice/81544a4989df4109b33c2d65037c5836
#include <ctype.h> #include <stdio.h> #include <string.h> // 1. 得到整行字符串 // 2. 处理字符串 // 2.1 逆序遍历字符串,将非字母的字符都设为空字符 // 2.2 每在字符串中找到一个单词就输出 int main() { char line[10010] = {0}; gets(line); int len = strlen(line) - 1; while(len >= 0) { if( ! isalpha(line[len])) { line[len] = '\0'; //非字母,检查它后一个是不是字母,若是字母,则它是一个单词的首字母 if(isalpha(line[len+1])) { printf("%s ", &line[len+1]); } } len --; } //处理首字符 if(isalpha(line[0])) { printf("%s", line); } return 0; }