酷代码 AI
菜单
服务商

C 语言实现句子反转:单词大小写转换与数字倒转

以下是使用 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; } ``` ######[AI写代码神器 | 393点数解答 | 2024-11-11 16:45:21]

相关提问
本站限时免费提问中
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]