//在源字符串中删除子字符串的函数
/* 可以处理空字符串,以及源串中有重复子串的情况 */
#include <stdio.h>
#define TRUE 1
#define FALSE 0
int del_substr( char *str, char const *substr )
{
char *s;
char const *t;
int status = FALSE;
int num = 0;
for ( s = str, t = substr; *s != '\0'; )
{
if ( *s != *t )
{
t = substr;
status = FALSE;
}
if ( *s == *t )
{
status = TRUE;
s++, t++;
}
if ( status == FALSE )
{ /* 一直不等才s++ */
s++;
}
if ( *t == '\0' && status == TRUE )
{ /* 发现子串即删除 */
num++; /* 发现子串次数计数 */
do
{
*( s - ( t - substr) ) = *s;
} while ( *s++ != '\0' );
s = str, t = substr; /* 复位 */
}
}
if ( num == 0 )
return FALSE;
else
return TRUE;
}
int main(int argc, char* argv[])
{
char *src = "ABCD MBCD EF";
char *sub = "BCD";
if ( del_substr( src, sub ) == FALSE )
printf( "not found substr: %s\n", sub );
else
printf( "del substring is: %s\n", src );
return 0;
}
在源字符串中删除子字符串的函数
最新推荐文章于 2021-11-22 17:56:37 发布