去掉fgets的换行符
时间: 2023-10-10 22:06:59 浏览: 313
要去掉fgets函数录入的字符串末尾的换行符'\n',可以使用一个自定义的函数来实现。可以定义一个名为"removeNewline"的函数,接受一个字符串作为参数,然后遍历字符串找到末尾的换行符'\n',将其替换为字符串结束符'\0'即可。这样就可以去掉fgets函数录入的换行符。代码示例如下:
```c
void removeNewline(char *str) {
int len = strlen(str);
if (len > 0 && str[len-1 == '\n') {
str[len-1 = '\0';
}
}
```
使用这个函数可以快速去掉fgets函数录入字符串末尾的换行符'\n'。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [去掉fgets拷贝的回车符](https://blog.csdn.net/wanggong_1991/article/details/123845340)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [封装fgets函数,去掉其末尾的换行符"\n](https://blog.csdn.net/qq_43968080/article/details/84862198)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文