c++ - 看不懂这段c语言代码的思路,目的是删除字符串1中与字符串2相同的字符
PHPz
PHPz 2017-04-17 13:06:30
0
2
763

代码:

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

学习是最好的投资!

reply all(2)
大家讲道理

It feels like there is no value assigned to j at all

for(i=j=0;str1[i];i++)

I don’t know what str1[j++]=str1[i] wants to do. . . Ask God for help

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

str1 = abcbd
str2 = b

step 1. [i][j]abcbdrrreee
step 2. a[i][j]bcbdrrreee
step 3. a[j]b[i]cbdrrreee
step 4. ac[j]c[i]bdrrreee
step 5. ac[j]cb[i]drrreee
step 6. acd[j]bd[i]rrreee
step 7. acdrrreee[j]drrreee[i]

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. . .

#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++);//这句代码为了找出str1[i]==str2[k]
        if(str2[k] == 'rrreee')//如果在str2中发现有与str1中相同的,但是却是'rrreee',那就代表其实str2中没有与str1中相同的字符
            str1[j++] = str1[i];//那么j就可以写入
        //else 出现了相同的就舍弃,j不写入
    }
    str1[j] = 'rrreee';//所有str1中的字符都检查过了,就添加一个'rrreee'收尾
    return str1;
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template