代码:
char *eliminate(char str1[], char str2[])
{
int i, j, k;
for(i=j=0;str1[i];i++)
{
for(k=0;str2[k] && str1[i]!=str2[k];k++); if(str2[k]== ‘\0’)
str1[j++]=str1[i];
}
str1[j]=‘\0’;
return str1;
}
感觉根本就没给j赋值啊,也不知道str1[j++]=str1[i]是想做什么。。。求助大神
原问题:
• Write a program to read two strings as input and remove from string
1 all characters contained in string 2
• Example:
str1: “Olimpico”
str2: “Oio”
result: “lmpc”
答案全部代码
#include < stdio.h >
#define MAXCAR 128
char * eliminate(char str1[], char b[]);
int main() {
char str1[MAXCAR], str2[MAXCAR];
printf(“Give me a string str1: ”);
scanf(“ % s”, str1);
printf(“Give me a string str2: ”);
scanf(“ % s”, str2);
printf(“str1 - str2 = % s\ n”, eliminate(str1, str2));
return 0;
}
char * eliminate(char str1[], char str2[]) {
int i, j, k;
for (i = j = 0; str1[i]; i++) {
for (k = 0; str2[k] && str1[i] != str2[k]; k++);
if (str2[k] == ‘\0’)
str1[j++] = str1[i];
}
str1[j] = ‘\0’;
return str1;
}
for(i=j=0;str1[i];i++)
In fact, it is very simple. The idea of the code is to process str1 byte by byte, i is the position being read, and j is the position being written. If the current character of i already exists in str2, then j can overwrite it, so ++ is not used. So you only need to use j++ when writing, and i is always ++ when writing. This is the meaning of this code.
Give me an example
That’s it. Although the string in str1 is acd
I tried your code but it didn’t work. It should be that some Chinese and English characters were mixed up
I helped you change the code and ran it
The ideas are all written in the comments
Your ID. . .