c - 将字符串中的字符串替换为字符

标签 c gets puts

如何编写用户定义的函数,该函数搜索另一个字符串中包含的任何字符并将其替换为字符串。 不能在代码中使用任何字符串变量,必须是用户定义的函数。 谢谢 这是我到目前为止所尝试过的 #define _CRT_SECURE_NO_WARNINGS #包括 #包括

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 中包含的任何字符替换为任何字符(例如“$”)

*/

最佳答案

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

关于c - 将字符串中的字符串替换为字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33538659/

相关文章:

c - 用于分配输出的 mexCallMATLAB 语法

c - gets() 在 while 循环中只接受一次输入

c - 从用户获取输入字符串后,输出包含一些其他字符

c - 为什么 puts 在以下代码中不起作用?

c - 嵌套的 TAILQ 双自由错误

c - 为什么这段代码打印第 n 个数字给出运行时错误?

c++ - 如何使用棋盘找到两个摄像机之间的旋转/平移

c - 如何禁用有关在 GCC 中使用弃用获取的警告?

c - 执行程序时出错

c - C中的printf()和puts()有什么区别?