#include<stdio.h>
#include<string.h>
#define MAX 100
/*
串str存在,
pos是字符的位置,从1开始;1 <= pos <= strlen(str) - len + 1
len是将要删除的子串长度
strdelete函数作用:
从串str中删除第pos个字符起长度为len的字串
*/
void strdelete(char *str, int pos, int len) {
int i, j = 0;
if (pos < 1 || pos > strlen(str) - len + 1)
return 0;
/*遍历串str,当下标在pos-1和pos+len-2之间时,跳过*/
for (i = 0; i < strlen(str); i++) {
if (i >= pos - 1 && i <= pos + len - 2)
continue;
str[j++] = str[i];
}
str[j] = '\0';
printf("%s", str);
}
int main(void) {
char str[MAX];
int pos, len;
scanf("%s %d %d", str, &pos, &len);
strdelete(str, pos, len);
return 0;
}
删除指定长度字符串
本文探讨了在编程中如何高效地删除字符串中的指定长度子串,包括使用内置函数、正则表达式等方法,并提供了实例代码进行解析。

被折叠的 条评论
为什么被折叠?



