用字符替换字符串中的字符串
问题描述:
如何对用户定义的函数进行编码,该函数用字符串搜索并替换另一个字符串中包含的任何字符的字符出现。 不能在代码中使用任何字符串变量,必须是用户定义的函数。 感谢 这就是我试图到目前为止 的#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
*/
答
//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;
}
+1
您建议的“替换”仅仅是复制。什么是'len'?分号过时了吗?而“获取”并不是21世纪的阅读方式。 –
+0
感谢您的帮助,但我试图避免字符串库函数像strlen和不使用指针。 –
呀,启动你的啦ptop,使用文本编辑器并开始输入。这就是你写代码的方式。疑问? –
你试过了什么?至少在SO中寻找类似的问题。我相信有很多。 – SKD
他们有很多,但他们使用替换函数或任何其他字符串库函数,如strlen等,我不得不在这个程序中使用。 –