C语言写一个函数,从一个字符串中的第p个位置起删除指定长度的字符串,在main中输出最终结果,若输入不合法,则输出 Illegal input,输入一个字符串s、位置p、长度n 输出 删除子串后的字符串
时间: 2024-06-13 12:07:11 浏览: 105
以下是一个C语言函数的例子,用于从一个字符串中的第p个位置起删除指定长度的字符串,并在main函数中输出最终结果:
```c
#include <stdio.h>
#include <string.h>
void deleteSubstring(char *str, int p, int n) {
int len = strlen(str);
if (p < 0 || p >= len || n < 0 || p + n > len) {
printf("Illegal input\n");
return;
}
memmove(str + p, str + p + n, len - p - n + 1);
}
int main() {
char str[100];
int p, n;
printf("输入字符串:");
gets(str);
printf("输入位置p:");
scanf("%d", &p);
printf("输入长度n:");
scanf("%d", &n);
deleteSubstring(str, p, n);
printf("删除子串后的字符串:%s\n", str);
return 0;
}
```
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![application/pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![7z](https://img-home.csdnimg.cn/images/20241231044736.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![text/x-c](https://img-home.csdnimg.cn/images/20250102104920.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)