How to delete a keyword from a text in C?

2

I have an exercise that says:

Encode a program that reads through a keyboard a keyword of up to 15 characters and a text of up to n lines (n being a constant value). The program should eliminate from the text those lines that contain the keyword and print the "modified" text on the screen.

I have more or less an idea of how to save the keyword and the text by lines.

This is the code I have:

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

#define MAX 4 //Este es el nº máximo de líneas

int main()
{
int i;

char clave[15];
char linea[20];
char *texto[MAX]; //array dinámico para el texto

printf("Introduce la palabra clave\n");
gets(clave);

for(i=0; i<MAX; i++)
    texto[i]=NULL; //Inicializo a NULL

printf("Texto original\n=============\n");

for(i=0; i<MAX; i++)
{
    printf("Introduce la %dº linea:\n",i);
    gets(linea);//leo las lineas del texto

    if(strstr(linea,clave)==NULL)
    {
        texto[i]=(char*) malloc(strlen(linea)+1);//guardo bloque 
        strcpy(texto[i],linea);//le adjudico las lineas al texto
    }
}

From here I am stuck, How do I compare the keyword and identify it in the text lines to eliminate it, and print the modified text on the screen again?

    
asked by AguaSal 03.01.2017 в 12:58
source

2 answers

5

You need to know which lines you are discarding and add to the texto array only the lines that do not contain the keyword.

The first change I've made in your code is cosmetic:

printf("Introduce la %dº linea:\n",i+1); // REV:

I have modified i by i+1 so that you do not show the text in the output.

To solve your problem, I have defined the variable j , which initialized 0 and will increase every time you add a line to the array texto .

int i, j = 0;

Later, within the if that compares if the key exists within the entered line, I assign the reserved buffer to the text line j

texto[j]=(char*) malloc(strlen(linea)+1);//guardo bloque 

I copy the line entered in texto[j] and increase the value of j once copying is done.

strcpy(texto[j++],linea);//le adjudico las lineas al texto

At the end of the code, I print the array with the added lines:

printf( "Resultado: \n" );
for(i=0; i<j; i++ ) 
{ 
    printf( texto[i] );
    printf( "\n" );
}

CONSIDERATIONS

  • As mentioned, the reserved chains are not being released
  • The gets function is very dangerous, it is easy to get a Buffer Overflow, it should be changed by fgets(str, 19, stdin); .
  • There are other considerations, such as the initialization loop of the array texto , which is not necessary.

I'll give you the complete code:

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

#define MAX 4 //Este es el nº máximo de líneas

int main()
{
    int i, j = 0;

    char clave[15];
    char linea[20];
    char *texto[MAX]; //array dinámico para el texto

    printf("Introduce la palabra clave\n");
    gets(clave);

    for(i=0; i<MAX; i++)
        texto[i]=NULL; //Inicializo a NULL

    printf("Texto original\n=============\n");

    for(i=0; i<MAX; i++)
    {
        printf("Introduce la %dº linea:\n",i+1); // REV:

        if(strstr(linea,clave)==NULL)
        {
            printf( "Copiando %s en la linea %d\n", linea, i );
            texto[j]=(char*) malloc(strlen(linea)+1);//guardo bloque 
            strcpy(texto[j++],linea);//le adjudico las lineas al texto
        }
    }

    printf( "Resultado: \n" );
    for(i=0; i<j; i++ ) 
    { 
        printf( texto[i] );
        printf( "\n" );
    }

}
    
answered by 03.01.2017 / 13:24
source
3

Well, you should compare each line with the key and with strstr check if it returns a value or not, in case the line will not contain the key. I'll give you the example for a line:

if(strstr(linea,clave) != NULL) {
printf( "La linea \"%s\" contiene la clave %s\n", linea,clave );
}
else
{
printf( "La linea \"%s\" no contiene la clave %s\n", linea,clave );
}
  

The char * strstr (const char * haystack,   const char * needle) searches for the first occurrence of the sub-string needle in the haystack string and returns a pointer to that occurrence and null if it does not find a match.

More info

    
answered by 03.01.2017 в 13:22