简体   繁体   English

比较2个字符串之间的元素

[英]Comparing elements between 2 strings

Hello, let's say I got 2 strings, "Today is a nice day" and "ao". 您好,假设我有2个字符串,“今天是美好的一天”和“ ao”。 I want to delete the chars of the 2nd string that appear in the 1st one. 我想删除出现在第一个字符串中的第二个字符串的字符。

This is my issue: 这是我的问题:

char c[20];
char p[10];
int i,j;
int l1,l2;

printf("Enter a string \n");
scanf("%s",cd);

printf("Enter another string \n");
scanf("%s",car);

len1 = strlen(cd);
len2 = strlen(car);

for (i=0;i<len1;i++){
    for (j=0;j<len2;j++){
        if (cd[i]==car[j]){
            cd[i]="";
        }
    }
}

What I want is the 1st string to be like "Tdy is nice dy". 我想要的是第一个字符串,例如“ Tdy is nice dy”。 So I empty the positions where the elements are the same to reposition it later. 因此,我清空了元素相同的位置,以便稍后重新定位。

Apparently "cd[i]==car[j]" can't be done on C, I got "Invalid conversion from 'const char*' to 'char'. 显然,“ cd [i] == car [j]”无法在C上完成,我收到了“从'const char *'到'char'的无效转换。

So i'm pretty much stuck. 所以我几乎陷入了困境。 I'll thank any help. 我将感谢您的帮助。

1) This is a solution matching your algorithm as close as possible. 1)这是一个尽可能与您的算法匹配的解决方案。 All what you need is an extra loop and to replace cd[i]=""; 您所需要的只是一个额外的循环并替换cd[i]=""; which cannot be compiled with cd[i]=0; 不能用cd[i]=0;编译cd[i]=0; . The error given by the compiler relates to expression cd[i]=""; 编译器给出的错误与表达式cd[i]=""; cd[i] is a character type and you cannot assign string "" which has a type const char * to char variable. cd[i]是字符类型,不能将类型为const char *字符串""分配给char变量。 cd[i] is a character "" is a pointer. cd[i]是字符""是指针。

The operation cd[i]=0; 运算cd[i]=0; gives you want you wanted: I empty the positions where the elements are the same to reposition it later. 给您想要的东西: 我清空元素相同的位置,以便以后重新定位。 It replaces the unwanted characters with 0. 它将不需要的字符替换为0。

#include <stdio.h>
#include <string.h>

int main()
{
    char cd[]  = "Today is a nice day";
    char tmp[] = "Today is a nice day";
    char car[] = "ao";
    int i;
    int j;
    int k;

    int len1 = strlen(cd);
    int len2 = strlen(car);

    for (i=0;i<len1;i++){
        for (j=0;j<len2;j++){
            if (cd[i] == car[j]){
               cd[i]=0;
          }
      }
    }

   k = 0;
   for (i=0; i<len1; i++)
   {  
        if(cd[i] == 0)
        {
        }
        else
        {
            tmp[k] = cd[i];
            k++;
        }
    }      
    tmp[k] = 0; /* remember to terminate the tmp */


    printf("%s\n", tmp);

    strcpy(cd,tmp);
    printf("%s\n", cd); 

    return 0;
}

OUTPUT: 输出:

Tdy is  nice dy                                                                                   
Tdy is  nice dy 

Alternatively, instead of clearing unwanted character with 0 you could just skip it. 另外,您也可以跳过不使用0清除不需要的字符的方法。 This solution is given below: 解决方案如下:

#include <stdio.h>
#include <string.h>

 int main()
    {
        char cd[]  = "Today is a nice day";
        char car[] = "ao";
        int i;
        int j;
        int k = 0;
        int skip = 0;


        int len1 = strlen(cd);
        int len2 = strlen(car);

        for (i=0; i<len1; i++)
        {
            for (j=0; j<len2; j++)
            {
                if (cd[i] == car[j])
                {
                    skip++; // make note that this character is not needed
                }
            }

            if(skip == 0)
            {
                cd[k] = cd[i]; // copy the character
                k++; // increase the position index
            }
            else
            {
                // skip the copy of charcter; clear the skip marker
                skip = 0;
            }              
        }

        cd[k] = 0; // remember to terminate the new ck string!          
        printf("%s\n", cd); 

        return 0;
    }  

OUTPUT: 输出:

Tdy is  nice dy  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM