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