简体   繁体   English

用字符替换字符串中的字符串

[英]replacement of string from a string by a character

how to code a user defined function that searches and replaces a character occurrences of any of the character contained in another string with a character string.如何编写用户定义的函数,该函数搜索并用字符串替换另一个字符串中包含的任何字符的字符出现。 Cannot used any string variable in the code, has to be a user defined function.不能在代码中使用任何字符串变量,必须是用户定义的函数。 Thanks This is what i have tried so far #define _CRT_SECURE_NO_WARNINGS #include #include谢谢这是我迄今为止尝试过的#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);
}

/* I just need to make another function that searches s1 and replaces any occurrence of any of the character contained is s2 with a character that can be anything(eg '$') /* 我只需要创建另一个函数来搜索 s1 并将包含的任何字符的任何出现替换为 s2 用一个可以是任何字符的字符(例如“$”)

*/ */

//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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM