Function in C that deletes a given string

Asked

Viewed 242 times

2

I need to create a function that deletes in s1 the first occurrence of s2.

char *StrlDelStr(char *s1,char *s2)

Example:

char *s = "O rato roeu a rolha da garrafa";
StrlDelStr(s, "xy"); -> "O rato roeu a rolha da garrafa"
StrlDelStr(s, "ra"); -> "O to roeu a rolha da garrafa"

My code is like this:

char *StrlDelStr(char *s1,char *s2)
{
    int i, j;
    for(i = 0, j = 0; i < strlen(s1); i++)
    {
        for(j = 0; j < strlen(s1); j++)
        {
            if(s1[i] == s2[j])
                s1[i] = ' ';
        }
    }
    return s1;
}

int main()
{
    char palavras[100] = "Hello World";
    char palavra2[100] = "lo";

    StrlDelStr(palavras, palavra2);
    printf("\n%s\n", palavras);
    return 0;
}

Is exhibiting "He W r d". I need a function where instead of replacing it with ' ' all occurrences of character/string, just erase where the string specific (s2).

  • Your search for the second string is wrong. Also after locating the second string in the first you don’t have to replace it with space but move as many positions as the string total provided in the second parameter.

2 answers

3


Erase, there’s no way. There is no way to make the variables in the positions you want, for example at position 6,7,8 simply cease to exist, what you can do is identify where it occurs s2 (in this case, only the first time), and copy s1 to the last character before the first occurrence of s2, then continue copying from s1 from the first character following s2 which is contained in s1 to the end.

#include<stdio.h>
#include<string.h>
#define TAMANHO 100
char *StrlDelStr(char *s1,char *s2)
{
  char *pt_pos = strstr(s1, s2);
  if(pt_pos == NULL) printf("s1 nao contem s2.\n");
  else
  {
    char aux1[TAMANHO], aux2[TAMANHO];
    *pt_pos=0;
    strcpy(aux1, s1); // a cópia para em pt_pos
    strcpy(aux2, pt_pos+(int)strlen(s2)); // a cópia para no antigo final de s1
    //printf("%s\n%s", aux1,aux2); // para testes
    strcat(aux1, aux2);
    strcpy(s1, aux1);
  }
  return s1;
}

int main(void)
{
    char palavras[TAMANHO] = "O rato roeu a rolha da garrafa";
    char palavra2[TAMANHO] = "ra";

    StrlDelStr(palavras, palavra2);
    printf("\n%s\n", palavras);
    return 0;
}

See working on repl it..

2

The accepted answer has something that works, but it is not efficient having several hidden loops that make the execution very slow, so it does the same efficiently (this is a mini parser):

#include<stdio.h>

void StrlDel(char *s1, char *s2) {
    int i = 0;
    int notFound = 1;
    while (s1[i]) {
        int j;
        for (j = 0; s2[j] && s2[j] == s1[i + j]; j++);
        if (!s2[j] && notFound) {
            int k;
            for (k = i; s1[k + j]; k++) s1[k] = s1[k + j];
            s1[k] = 0;
            notFound = 0;
        } else i++;
    }
}

int main(void) {
    char palavras[] = "O rato roeu a rolha da garrafa";
    StrlDel(palavras, "ra");
    printf("\n%s\n", palavras);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.