编程实现删除字符串中所有给定的子串
2013-04-19 11:42
232 查看
#include <cstdio> #include <cstring> #include <cstdlib> /*删除指定字符*/ char *delchar(char *str, char c) { char *p = NULL, *head = NULL; p = head = str; if (str == NULL) return NULL; while (*p) { if (*p != c) *str++ = *p; p++; } *str = '\0'; return head; } /*删除指定子串*/ int delete_sub_str(const char *str, const char *sub_str, char *result_str) { const char *p = str; char *out = result_str; int len = strlen(sub_str); int count = 0; while (*p) { p = strstr(p, sub_str); if (p == NULL) //查找失败 break; memcpy(out, str, p-str); out += p-str; p += len; str = p; count++; } *out = '\0'; if (*str != '\0') strcpy(out, str); return count; } int main() { const char *src = "abcde123abcd123abv"; const char *sub = "123"; char *output = (char*)malloc(100); int num = 0; num = delete_sub_str(src, sub, output); puts(output); return 0; }
相关文章推荐
- 编程实现删除字符串中所有指定的字符
- 华为机试-删除字符串中所有给定的子串
- 编程实现删除字符串中所有指定的字符
- 删除字符串中所有给定的子串
- 华为机试 删除字符串中所有给定的子串
- 删除字符串中所有给定的子串
- 删除字符串中所有给定的子串
- 华为机试-删除字符串中所有给定的子串
- 华为机试 删除字符串中所有给定的子串
- 编程实现获取两个字符串中最大相同子串
- 编程实现查找两个字符串的最大公共子串 示例:"aocdfe"和"pmcdfa"最大公共子串为"cfd"
- 给定一个字符串,找到包含该字符串所有字符的最短子串
- Python实现统计给定字符串中重复模式最高子串功能示例
- 09给定任意俩组字符串S1和S2,请编程输出他们间的最大相同子串
- 给定任意俩组字符串S1和S2,请编程输出他们间的最大相同子串。
- 删除原字符串中所有匹配的子串,并返回匹配的子串个数
- 编程找出字符串的最长子串,要求子串的所有字符相同
- 顺序表来实现函数:删除最小值并返回删除元素,空出位置用尾部元素来填补,删除与给定值相同的所有的元素,删除给定值s,t(s<t)之间的元素
- c/c++整理--实现删除字符串所有指定字符
- 给定一个字符串,判断该字符串中是否包含某个子串.如果包含, 求出子串的所有出现位置