首页 > 试题广场 >

请按注释的说明,用C语言实现以下函数的功能。

[问答题]
请按注释的说明,用C语言实现以下函数的功能。
/*Name:replace
Function:to replace substring s1 in string source with substring s2
Parameters:source,string supposed to hold substring
s1,substring to be replaced
s2,substring to replace substring s1
flag,case-sensitive flag,
1,case-sensitive
0,non-case-sensitive
Return values: number of substrings haved been replaced*/
(Supplementary:To be not complicated,assume that the length of the string after replaced is not greater than 1024 bytes.)
 inline bool isEqual(char a, char b, int flag) {
	if (flag == 0)
		return toupper(a) == toupper(b);
	else
		return a == b;
}

int* getNextArray(char* pattern, int flag) {
	assert(pattern != NULL);
	int len = strlen(pattern);
	assert(len > 0);
	int *next = (int*)malloc(sizeof(int) * len);

	next[0] = -1;
	for (int i = 0, j = -1; i < len - 1; i++) {
		if (-1 == j || isEqual(pattern[i], pattern[j], flag)) {
			i++; j++;
			next[i] = j;
		}
		else {
			j = next[j];
		}
	}
	return next;
}


char* KMP(char* source, char* pattern, int flag) {
	int lenSource = strlen(source);
	int lenPattern = strlen(pattern);
	assert(lenPattern > 0);
	int* next = getNextArray(pattern, flag);
	int i, j;
	for (i = 0, j = 0; i < lenSource && j < lenPattern;) {
		if (-1 == j || isEqual(source[i], pattern[j], flag)) {
			i++; j++;			
		}
		else {
			j = next[j];
		}
	}
	free(next);
	if (j >= lenPattern) 
		return source + i - lenPattern;
	else
		return NULL;
}

void strncopy(char* dest, char* source, int n) {
	for (int i = 0; i < n; i++) {
		*dest++ = *source++;
	}
}

int replace(char* source, char* s1, char* s2, int flag) {
	int len1 = strlen(s1);
	int len2 = strlen(s2);
	int lenS = strlen(source);
	char* newBegin = KMP(source, s1, flag);
	if (newBegin == NULL)
		return 0;
	char newSource[1024];
	char *pSource = source, *pNewSource = newSource;
	int cnt = 0;
	while (newBegin != NULL)
	{
		cnt++;
		strncopy(pNewSource, pSource, newBegin - pSource);
		pNewSource += newBegin - pSource;
		pSource = newBegin + len1;
		strncopy(pNewSource, s2, len2);
		pNewSource += len2;
		newBegin = KMP(pSource, s1, flag);
	}
	while (*pSource != '\0') {
		*pNewSource++ = *pSource++;
	}
	*pNewSource = '\0';
	pSource = source; pNewSource = newSource;
	while (*pNewSource != '\0') {
		*pSource++ = *pNewSource++;
	}

	return cnt;
}
发表于 2015-11-14 19:30:32 回复(0)
用C写字符串替换函数,写不出来

发表于 2015-11-01 20:05:47 回复(0)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define   M  1024
#define    N 20

  void toUpper(int &c)
 {
   if(c>=97&&c<=122)
    c=c-32;
 }
 int isBegin(char *pos,char *s1,int flag)  //判断从pos开始的前缀串是否和s1匹配
{
    int length =strlen(pos);
    int length1=strlen(s1);
   int c1,c2;
    if(length<length1) return 0;

    for(int i=0;i<length1;i++)
  {
     c1=pos[i],c2=s1[i];
     if(flag==0)
      {           
           toUpper(c1);
          toUpper(c2);

      }
      if(c1!=c2)return 0;
      
   }   
   return 1;

}
int replace(char * source,char * s1, char *s2,int flag) //flag=1 区分大小写 0 不区分  返回替换个数
{
    
   int length = strlen(source);
   int length1=strlen(s1);
   int length2 =strlen(s2);
   int count = 0;
   int a[N]={-1};
   char * tmp =(char*)calloc(length+1,sizeof(char));
    strcpy(tmp,source);
    memset(source,0,M);
   for(int i=0;i<length;)
  {
      if(isBegin(&tmp[i],s1,flag))
      {
        a[count++]=i;
        i=i+length1;
      }
     else i++;
       
  }
 
int i=0,j=0, k=0;  // k是替换点的下标
 for( i=0;i<length;) //i是tmp的下标
 {
    if(a[k]==i)
    { strcpy(&source[j],s2);
      i=i+length1;
      j=j+length2;
      k++;
     }
    else
    {
      source[j]=tmp[i];
        i++;
       j++;
     }
 }
 
     return count;
 
 }
 
   
int main()
{
   char *source = (char*)malloc(M);
   char *s1 = (char*)malloc(N);
   char *s2 = (char*)malloc(N);

   printf("输入source字符串:\n");
   gets(source);
      printf("输入s1字符串:\n");
   gets(s1);
  printf("输入s2字符串:\n");
   gets(s2);
int i=  replace(source,s1,s2,0);
 printf("%s   共替换%d处",source,i);    
   return 0;
}
发表于 2015-05-20 16:25:16 回复(2)