-5

如何编写用户定义的函数来搜索并用字符串替换另一个字符串中包含的任何字符的字符出现。不能在代码中使用任何字符串变量,必须是用户定义的函数。谢谢这是我迄今为止尝试过的#define _CRT_SECURE_NO_WARNINGS #include #include

void s1();
void s2();

int main(void)
{   

    int i=0;


    s1();
    s2();
    printf("c = {'$'} ");

}//main
void s1(){
    int i = 0;
    while (i <= 40){
    printf("%c", (rand() % 25) + 'A');
    i++;
    }
}
void s2(){
    char s2[20];

    printf("\nEnter a string of minimum 2 and maximum 20 characters= ");
    gets(s2);
    puts(s2);
}

/* 我只需要创建另一个函数来搜索 s1 并用可以是任何字符的字符(例如“$”)替换任何出现的包含 s2 的任何字符

*/

4

1 回答 1

0
//If I have understood your question then this should be answer
char *replace(char [] a, char b[], int lower, int upper){
  char c[100];
  int j = 0;
  for(int i = 0; i < lower; i++){
     c[j] = a[i];
     j++;
  }
  for(int i = 0; i < strlen(b); i++){
     c[j] = b[i];
     j++;
  }
  for(int i = upper; i < strlen(a); i++){
     c[j] = a[i];
     j++;
  }
  c[j] = '\0'

  for(int i = 0; i < strlen(c); i++){
    a[i]= c[i];
  }  
  a[i] = '\0';  
  return a;
}
于 2015-11-05T07:25:05.087 回答