子串删除程序, 抛砖引玉

华亭真人 华亭真人 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *temp;
void del_sub_str(char**s, char* sub)
{
int len1 = strlen(*s);
int len2 = strlen(sub);
if(len1 < len2)
return;
if(strcmp(*s, sub) ==0)
{
strcpy(*s, "");
return;
}
strcpy(temp, *s);
int i ;
for( i = 0; i < len1 - len2; i++)
{
if(*temp == *sub)
{
char *t = sub;
while(*t!='\0'&&(*t++ == *temp++));
if(*t == '\0')
{
break;
}
}
temp++;
}
if(i >= len1 - len2 )
{
return;
}
strcpy((*s)+(len1 - strlen(temp) - len2), temp);
del_sub_str(s, sub);
}

void main()
{
char *s = new char[1024];
temp = new char[1024];
char *t = temp;
strcpy(s, "abcdabbaabbkk");
del_sub_str(&s, "ab");
printf("%s\n", s);
delete s;
delete t;
}
...全文

相关推荐

评论 9
收藏 已收藏
分享
返回顶部
华亭真人
C++ 语言 65,214 成员 250,514 内容