69,368
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
void get_input_str();
void squeeze(char s1[], char s2[]);
int main(int argc, char *argv[])
{
get_input_str();
}
void get_input_str()
{
char str1[30],str2[30];
printf("请输入str1字符串:\n");
scanf("%s",str1);
printf("请输入str2字符串:\n");
scanf("%s",str2);
squeeze(str1, str2);
}
void squeeze(char s1[], char s2[])
{
int i, j, k;
for (i = k = 0; s1[i] != '\0'; i++)
{
for (j = 0; s2[j] != '\0' && s2[j] != s1[i]; j++);
if(s2[j] == '\0')
s1[k++] = s1[i];
s1[k] = '\0';
}
printf("%s\n",s1);
}
#include <stdio.h>
void get_input_str(); //接受输入.
void squeeze(char s1[], char s2[]);
int main(int argc, char *argv[])
{
get_input_str();
}
void get_input_str()
{
char str1[30],str2[30];
printf("请输入str1字符串:\n");
scanf("%s",str1);
printf("请输入str2字符串:\n");
scanf("%s",str2);
squeeze(str1, str2);
}
void squeeze(char s1[], char s2[])
{
int i, j, k;
for (i = 0; s1[i] != '\0'; i++)
{
for (j = 0; s2[j] != '\0'; j++)
{
if(s1[i] == s2[j])
{
for (k = i; s1[k] != '\0'; k++)
{
s1[k] = s1[k+1];
}
}
}
}
printf("%s",s1);
}
/***********************************************************************
* Function : char *strrmv3(char *s1, char *s2);
* Author : loops
* Date : 2007.11.28
* Description: Remove sub string s2 from s1 without invoking any other functions.
* Maximum copy times: strlen(s1); Maximum scan times: strlen(s1)
* Assembly instructions: 49
* Comments : Compared to strrmv2, the overhead of strrmv3 comes from the calculation
: of (p-s2) along with every successful match.
***********************************************************************/
char* strrmv3( char *s1, char *s2)
{
char *w=s1, *r=s1, *p;//w: 写入位置,r: 读入位置 p: 用于处理s2
for( p=s2; *r; *w++=*r++ )
!(*p)&&(w-=(p-s2),p=s2), (*r!=*p)?(p=s2):(p+=1);
*p ? (*w='\0') : (*(w-=(p-s2))='\0');//如果*p=='\0',说明s1末尾匹配成功了。
return s1;
}
int main()
{
char str[]="abcabcdefgabcdefgabcdefgabcabcdefgabc";
printf("%s\n",strrmv3(str,"abc"));
return 0;
}
/***********************************************************************
* Function : char *strrmv2(char *s1, const char *s2);
* Author : loops
* Date : 2007.11.28
* Description: Remove sub string s2 from s1 without invoking any other functions.
* Maximum copy times: strlen(s1); Maximum scanning characters: strlen(s1)
* Assembly instructions: 53
***********************************************************************/
char* strrmv2( char *s1, const char *s2)
{
size_t dstLen=0;
char *w=s1, *r=s1;//w: 写入位置,r: 读入位置
const char *p; //p: 用于处理s2
for( p=s2;*p; ++dstLen, ++p );//得到s2的长度
for( p=s2; *r; *w++=*r++ )
!(*p)&&(w-=dstLen,p=s2), (*r!=*p)?p=s2:p+=1;
*p ? *w='\0' : *(w-=dstLen)='\0';//如果*p=='\0',说明s1末尾匹配成功了。
return s1;
}
/***********************************************************************
* Function : char *strrmv3(char *s1, char *s2);
* Author : loops
* Date : 2007.11.28
* Description: Remove sub string s2 from s1 without invoking any other functions.
* Maximum copy times: strlen(s1); Maximum scanning characters: strlen(s1)
* Assembly instructions: 49
* Comments : Compared to strrmv2, the overhead of strrmv3 comes from the calculation
: of (p-s2) along with every successfully match.
***********************************************************************/
char* strrmv3( char *s1, char *s2)
{
char *w=s1, *r=s1, *p;//w: 写入位置,r: 读入位置 p: 用于处理s2
for( p=s2; *r; *w++=*r++ )
!(*p)&&(w-=(p-s2),p=s2), (*r!=*p)?p=s2:p+=1;
*p ? *w='\0' : *(w-=(p-s2))='\0';//如果*p=='\0',说明s1末尾匹配成功了。
return s1;
}
int main()
{
char str[]="abcabcdefgabcdefgabcdefgabcabcdefgabc";
printf("%s\n",strrmv(str,"abc"));
return 0;
}
/***********************************************************************
* Function : char *strrmv(char *s1, const char *s2);
* Author : loops
* Date : 2007.11.28
* Description: Remove sub string s2 from s1 without invoking any other functions.
***********************************************************************/
char* strrmv( char *s1, const char *s2)
{
size_t dstLen=0;
char *w=s1, *r=s1;//w: 写入位置,r: 读入位置
const char *p; //p: 用于处理s2
for( p=s2;*p; ++dstLen, ++p );//得到s2的长度
for( p=s2; *w && *p; (*w++!=*p++) && (p=s2) );//寻找到s1中第一个匹配s2的位置
for( p=s2, r=w,w-=dstLen; *r; *w++=*r++ )
!(*p)&&(w-=dstLen,p=s2), (*r!=*p)?p=s2:p+=1;
*p ? *w='\0' : *(w-=dstLen)='\0';//如果*p=='\0',说明s1末尾匹配成功了。
return s1;
}
char* strrmv( char *s1, const char *s2)
{
size_t dstLen=0;
char *w=s1, *r=s1;//w: 写入位置,r: 读入位置
const char *p; //p: 用于处理s2
for( p=s2;*p; ++dstLen, ++p );//得到s2的长度
for( p=s2; *w && *p; (*w++!=*p++) && (p=s2) );//寻找到s1中第一个匹配s2的位置
for( p=s2, r=w,w-=dstLen; *r; *w++=*r++ )
!(*p)&&(w-=dstLen), (*r!=*p)?p=s2:p+=1;
*p ? *w='\0' : *(w-=dstLen)='\0';//如果*p=='\0',说明s1末尾匹配成功了。
return s1;
}
http://www.cyberspace.org/~jhl/miscc.c
/*******************************************************************************
* Function : char *strrmv(char *s1, const char *s2);
* Author : lovecreatesbeauty@gmail_Del.com [Remove _Del for email]
* Date : 2007.11.27
* Description: Remove sub string s2 from s1.
******************************************************************************/
char *strrmv(char *s1, const char *s2)
{
int i, j, w = 0, r = 0;
for (i = 0; s1[i]; i++){
for (j = 0; s2[j]; j++)
if (s1[i + j] != s2[j]) break;
if (!s2[j] && j){
if (r) for (; r != i; s1[w++] = s1[r++]) ;
else w = i;
r = i + j, i = r - 1;
}
if (r && !s1[i + 1])
for (; r <= i + 1; s1[w++] = s1[r++]) ;
}
return s1;
}
http://www.cyberspace.org/~jhl/miscc.c
/*******************************************************************************
* Function : char *strrmv(char *s1, const char *s2);
* Author : lovecreatesbeauty@gmail_Del.com [Remove _Del for email]
* Date : 2007.11.27
* Description: Remove sub string s2 from s1.
******************************************************************************/
char *strrmv(char *s1, const char *s2)
{
int i, j, w = 0, r = 0;
for (i = 0; s1[i]; i++){
for (j = 0; s2[j]; j++)
if (s1[i + j] != s2[j])
break;
if (!s2[j]){
if (r) for (; r != i; s1[w++] = s1[r++]) ;
else w = i;
r = i + j, i = r - 1;
}
if (r && !s1[i + 1])
for (; r <= i + 1; s1[w++] = s1[r++]) ;
}
return s1;
}