"Fixing the Bugs in C Code: Correcting `gets`, `strlen`, and Pointer Usage"
There are several issues in the code provided. Here is the corrected version: ```c #include <stdio.h> #include <string.h> void sort(char *p); int main(void) { char str[21]; fgets(str, 21, stdin); sort(str); printf("%s\n", str); return 0; } void sort(char *p) { int i, len = strlen(p), ln = len - 2, j; char q[20]; strncpy(q, p + 1, len - 2); for (i = 0; i < len - 3; i++) { for (j = 0; j < ln - 1; j++) { if (*(q + j) < *(q + j + 1)) { char t = *(q + j); *(q + j) = *(q + j + 1); *(q + j + 1) = t; } ln--; } } strncpy(p + 1, q, len - 2); } ``` Changes made: 1. Used `fgets` instead of `gets` to prevent buffer overflow. 2. Included `<string.h>` header for `strlen` function. 3. Fixed the usage of pointers and arrays within the `sort` function. 4. Changed `puts` to `printf` for outputting the sorted string. ######[AI写代码神器 | 664点数解答 | 2024-12-06 09:27:31]
- SQL 查询纠错:精准修正排序子句,实现特定数据筛选与排序 (阿里通义 | 262点数解答 | 2024-05-14 11:17:48)345
- "SQL条件判断下的角色数据更新操作揭秘"(GPT | 51点数解答 | 2024-10-22 11:24:35)294
- "Enhanced SQL Script for SRO MMORPG Database: Conditional Updates, Transactions & Logging"(阿里通义 | 969点数解答 | 2024-10-22 11:26:02)357
- C++编程:输入整数判断奇偶与大小并按需输出(字节豆包 | 189点数解答 | 2025-12-13 20:11:55)73
- C++实现:输入整数判断奇偶与大小并输出对应结果(DeepSeek | 123点数解答 | 2025-12-28 14:53:27)52
- 用按键精灵与大漠插件还原CE脚本功能:游戏内存修改全攻略(字节豆包 | 1016点数解答 | 2026-01-21 14:27:19)43
- SQL查询纠错:“美国|英国|澳大利亚”LIKE子句通配符问题修正 (阿里通义 | 341点数解答 | 2024-09-29 21:37:36)280
- 中英对译:领略中国古代哲学与自然观的跨文化魅力(百度文心 | 522点数解答 | 2024-10-14 21:51:07)691
- Python实现简单文档内容查询:精准定位所需内容索引(GPT | 1189点数解答 | 2024-10-21 21:45:05)337
- Python 实现:轻松查询文档中特定内容的所有索引(字节豆包 | 261点数解答 | 2024-10-22 15:37:39)357
- Python 实现:轻松查询文档中指定内容的所有索引(字节豆包 | 259点数解答 | 2024-10-22 15:41:34)301
- Python实现:一键查询文档内容索引的简单程序 (GPT | 1356点数解答 | 2024-10-24 16:32:26)240