酷代码 AI
菜单
全部AI  /  热门  /  AI写代码神器
服务商

使用c| 7-1 句子反转 分数 30 作者 陈宇璟 单位 浙江大学 给定一行句子,每个词之间用空格隔开,要么是全小写英文单词,要么是全大写英文单词,要么是自然数。 要求将这些单词倒序输出。而且对于每个单词,如果是小写词,应当转为大写;如果是大写词,应当转为小写;如果是自然数,应该倒转输出。 举一个例子: we choose to go 2 the 123 moon 程序应当输出: moon 321 the 2 go to choose we 输入格式 仅一行,即需要反转的句子。 输出格式 仅一行,表示程序对句子的处理结果。 说明/提示 样例解释 首先应当按单词逆序,即: moon 123 the 2 go to choose we 小写变大写、大写变小写、倒转自然数之后,得到最终结果: moon 321 the 2 go to choose we 数据规模与约定 对于 100% 的数据,句子中包含的单词数量不超过 1000,每个单词长度不超过 6。 输入样例: we choose to go 2 the 123 moon 输出样例: moon 321 the 2

以下是使用 C 语言解决此问题的代码: ```c #include <stdio.h> #include <string.h> #include <ctype.h> void reverseWord(char *word) { int len = strlen(word); for (int i = 0; i < len / 2; i++) { char temp = word[i]; word[i] = word[len - i - 1]; word[len - i - 1] = temp; } } void processWord(char *word) { if (isdigit(word[0])) { reverseWord(word); } else if (islower(word[0])) { for (int i = 0; i < strlen(word); i++) { word[i] = toupper(word[i]); } } else if (isupper(word[0])) { for (int i = 0; i < strlen(word); i++) { word[i] = tolower(word[i]); } } } int main() { char sentence[10000]; fgets(sentence, sizeof(sentence), stdin); char *token = strtok(sentence, " "); char result[10000] = ""; while (token!= NULL) { processWord(token); strcat(result, token); strcat(result, " "); token = strtok(NULL, " "); } int len = strlen(result); if (len > 0) { result[len - 1] = '\0'; } printf("%s\n", result); return 0; } ``` [2024-11-11 16:45:21 | AI写代码神器 | 393点数解答]

相关提问