c语言插入字符到字符串中
时间: 2023-12-12 10:02:50 浏览: 282
在C语言中,可以使用标准库函数strcat()和strcpy()来插入字符到字符串中。下面是两种实现方法:
方法一:使用strcat()函数
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[50] = "Hello";
char ch = '!';
strcat(str, &ch); // 将字符追加到字符串末尾
printf("After inserting character: %s", str);
return 0;
}
```
输出结果:
```
After inserting character: Hello!
```
方法二:使用strcpy()和数组下标
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[50] = "Hello";
char ch = '!';
int len = strlen(str);
str[len] = ch; // 将字符插入到字符串末尾
str[len+1] = '\0'; // 在插入字符后添加字符串结束符
printf("After inserting character: %s", str);
return 0;
}
```
输出结果:
```
After inserting character: Hello!
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)