办公家具设计方案ppt:散分求两个C的用户自定义函数~急

来源:百度文库 编辑:高校问答 时间:2024/05/08 12:30:03
1.在指定的一个字符串中查找子字符串,返回值是查找到子字符串个数.
2.在指定的一个字符串中用新子字符串替换子字符串,返回值是替换的子字符串个数.
不能用C自带的string和Cstring类~
多谢了~
回答者:陈冠钢 - 助理 三级
谢谢了~可是要求不能用STRING类函数,strstr和strlen都不能用吧~~

//1.在指定的一个字符串中查找子字符串,返回值是查找到子字符串个数.
//2.在指定的一个字符串中用新子字符串替换子字符串,返回值是替换的子字符串个数.

#include<stdio.h>
#include<string.h>

/******************************************
输入参数:string,substring 查找string中有多少个substring,并返回个数。
******************************************/
int FindSub(char *string, char *substring)
{
int len=strlen(string),lensub=strlen(substring);
int i,j,same=0,sum=0;
for(i=0;i<len;i++)
{
if(string[i]==substring[0])
{
j=0;
same=0;
while((i+j)<len && j<lensub)
{
if(string[i+j]==substring[j])
same++;
j++;
}

if(same==lensub)
{
sum++;
i+=lensub;
}
}
}

return sum;

}

/******************************************
输入参数:string, substring, ReplaceString
将string中的substring替换为ReplaceString
******************************************/
int Replace(char *string, char *substring, char *ReplaceString)
{
int len=strlen(string),lensub=strlen(substring),lenrep=strlen(ReplaceString);
int i,j,p,sum=0,k=0;
char *find;

for(i=0;i<len;i++)
{
find=strstr(string,substring);
if(find!=NULL)
{
p=find-string;
if(lensub>lenrep)
{
sum++;
j=0;
while(ReplaceString[j]!='\0')
{
string[p+j]=ReplaceString[j];
j++;
}
j=0;
while(string[p+j+lensub]!='\0')
{
string[p+j+lenrep]=string[p+j+lensub];
j++;
}
string[p+j+lenrep]='\0';
}
else if(lensub<lenrep)
{
sum++;
for(j=len+(lenrep-lensub);j>=p+lensub;j--)
{
string[j]=string[j-(lenrep-lensub)];
}
k=0;
for(j=p;j<p+lenrep;j++)
{
string[j]=ReplaceString[k];
k++;
}
}
else if(lensub==lenrep)
{
k=0;
for(j=p;j<lensub;j++)
{
string[j]=ReplaceString[k];
k++;
}
}
string+=(p+lenrep);
}

}

return sum;
}

int main()
{
char string[1000],substring[20],ReplaceString[20];
int sum=0;
strcpy(string,"china china china china 123china:)china");
strcpy(substring,"china");
strcpy(ReplaceString,"good");
printf("\nstring: %s\nsubstring: %s\nReplaceString: %s",string,substring,ReplaceString);
sum=Replace(string,substring,ReplaceString);
printf("\n\n%s",string);
printf("\nsum=%d",sum);
return 0;
}

kmp算法实现 效率高
数据结构上有
给你先 ,不过有点文不对题哦 再改改
/* kmp 算法匹配字符串*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>

void Input(char *p);
void get_next(char mode_string[],int next[]);
int Index_KMP(char main_string[],char mode_string[]);
void display(char main_string[],char mode_string[],int key);

void main()
{
char judgement;
int key;
char main_string[100];
char mode_string[20];
Input(main_string);
Input(mode_string);
//clrscr();
key=Index_KMP(main_string,mode_string);
if(key==0)
{
//clrscr();
printf("not match successful.");
getch();
}
else{
//clrscr();
printf("match successful the position is: %d ",key);
getch();

}
printf("\ndo you want to watch it in main_string?(Y or N):");
judgement=getch();
if(judgement=='n'||judgement=='N') exit(0);
display(main_string,mode_string,key);

}/*main*/
void Input(char *p)
{
//clrscr();
printf("input the string:\n");
gets(p);
}
int Index_KMP(char main_string[],char mode_string[])

{
int i=0;
int j=0;
int key;
int lengthmain=strlen(main_string);
int lengthmode=strlen(mode_string);
int next[20];
get_next(mode_string,next);

while(1)
{

if(j==-1||main_string[i]==mode_string[j])
{
i++;
j++;
}
else
{ j=next[j];

}

if(i>=lengthmain||j>=lengthmode)

break;

}
if(j=strlen(mode_string))
return (i-strlen(mode_string)+1);
return 0;
}/*index_KMP*/

void get_next(char mode_string[],int next[])
{
int i=1;
int j=0;
next[0]=0;
while(i<strlen(mode_string))
{
if(j==0||mode_string[i-1]==mode_string[j-1])
{
i++;
j++;
next[i-1]=j;

}

else j=next[j-1];
}

for(i=0;i<strlen(mode_string);i++)
next[i]--;
}/*get_next*/

void display(char main_string[],char mode_string[],int key)

{
int i;
//clrscr();
for(i=0;i<=key-2;i++)
printf("%c",main_string[i]);
printf("\%");
for(i=0;i<strlen(mode_string);i++)
printf("%c",mode_string[i]);
printf("\%");
for(i=key+strlen(mode_string)-1;i<strlen(main_string);i++)
printf("%c",main_string[i]);
getch();

}