简体   繁体   中英

Remove string from string in C

I am making a program that gets 2 strings from the user then delete the occurrences of the second string from the first.

Directions of the program: The second string defined as any string of characters containing alphabets, special characters, numbers or even whitespaces, which may or may not be present in the original string. It can also be present multiple times in the same input string.

I can have strings up to 100 characters in length.

1) Taking the input strings one after the other from the user until the newline character is entered.

2) I want to write a function to check the occurrences of the pattern string in the first and then deletes it from the first one.

3) If I do find a string, return 1 otherwise return 0. It will print the resultant string with the characters removed if such exists.

Here is my code:

#include <stdio.h>
#define STRING_SIZE 100


int remover(char *s1, char *s2, char *s3)
{
   int i = 0, j, k,t=0;
   while (s1[i])    
   {
       for (j = 0; s2[j] && s2[j] == s1[i + j]; j++);  
          if (!s2[j])          
             {
                 for (k = i; s1[k + j]; k++)  
                    s1[k] = s1[k + j];
                    s1[k] = 0;
                    s3[t]=s1[k + j];
                    t++;
              }
          else
              i++;    
    }
    if(strlen(s2)>1){
        return 1;
    }
    return 0;
}


int main() {
    char result_string[STRING_SIZE];
    char MainString[STRING_SIZE], PatternString[STRING_SIZE];

    printf("Please enter the main string..\n");
    fgets(MainString,STRING_SIZE,stdin);


    printf("Please enter the pattern string to find..\n");
    fgets(PatternString,STRING_SIZE,stdin); 


    int is_stripped = remover(MainString,PatternString,result_string); // Your function call here..


    printf("> ");
    printf(is_stripped ? result_string : "Cannot find the pattern in the string!");
    return 0;
}

It doesn't work:(

Some examples of right output:

Please enter the main string..                                                                                       
This is an example string.                                                                                                               
Please enter the pattern string to find..                                                                            
ple str 
>This is an examing.

Please enter the main string..                                                                                       
This is an example input string.                                                                                                               
Please enter the pattern string to find..                                                                            
Bazinga
>Cannot find the pattern in the string!

Please enter the main string..                                                                                       
A string is string.                                                                                                               
Please enter the pattern string to find..                                                                            
str 
>A ing is ing.

**In this case, the pattern string was empty:**
Please enter the main string..                                                                                       
This is an example input string.                                                                                                               
Please enter the pattern string to find..                                                                            

>Cannot find the pattern in the string!

**In this case, the input was whitespace:**
Please enter the main string..                                                                                       
This is an input. This is after period.                                                                                                               
Please enter the pattern string to find..

Thisisaninput.Thisisafterperiod.

char *strdelstr(char *s1, char *s2) {
  char *loc = strstr(s1, s2);

  if(loc != NULL)
    strcpy(loc, loc + strlen(s2));

  return s1;
}

It's very easy. But there may be better ways to do that.

I have modified your code a bit.

int remover(char *s1, char *s2, char *s3)
{
   int i = 0, j,t=0,found;

   while (s1[i])
   {
       found=1;//Initilize found to true
       for (j = 0;s2[j]!=0; j++){
        if(s1[i+j]!=s2[j])
            found=0;//Set not found
       }
       if(found==0){
        s3[t]=s1[i];// if not found add char to s3.
        t++;
       }
       else{
        i=i+strlen(s2)-1;//if found skip
       }
              i++;
    }
    s3[t]=0;
    if(strlen(s1)>strlen(s3)){
        return 1;
    }
    return 0;
}

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