简体   繁体   中英

Comparing elements between 2 strings

Hello, let's say I got 2 strings, "Today is a nice day" and "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". 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'.

So i'm pretty much stuck. I'll thank any help.

1) This is a solution matching your algorithm as close as possible. All what you need is an extra loop and to replace cd[i]=""; which cannot be compiled with cd[i]=0; . The error given by the compiler relates to expression cd[i]=""; cd[i] is a character type and you cannot assign string "" which has a type const char * to char variable. cd[i] is a character "" is a pointer.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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