Keyword in String C language

3

I'm trying to make a program in C to find a word in a string vector, returning the position of the word and when it does not find returning -1. I am having problems with substring , type my program identifies bits of words, how to fix this?

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

int match(char [], char []);

int main() {
  char a[100], b[100];
  int posicao;

  printf("entre um texto\n");
  gets(a);

  printf("entre a palavra que quer procurar\n");
  gets(b);

  posicao = match(a, b);

  if(posicao != -1) {
    printf("%d\n", posicao );
  }
  else {
    printf("-1\n");
  }

  return 0;
}

int match(char texto[], char padrao[]) {
  int c, d, e, tamanho_texto, tamanho_padrao, posicao = -1;

  tamanho_texto    = strlen(texto);
  tamanho_padrao = strlen(padrao);

  if (tamanho_padrao > tamanho_texto) {
    return -1;
  }

  for (c = 0; c <= tamanho_texto - tamanho_padrao; c++) {
    posicao = e = c;

    for (d = 0; d < tamanho_padrao; d++) {
      if (padrao[d] == texto[e]) {
        e++;
      }
      else {
        break;
      }
    }
    if (d == tamanho_padrao) {
      return posicao;
    }
  }

  return -1;
}
    
asked by anonymous 22.05.2016 / 04:32

2 answers

0

Do not use the gets() function because the function does not receive the buffer size and a buffer overflow can occur. I used fgets and your program worked, I just changed a few things, but nothing in logic.

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

#define BUF_SIZE 100

int match( const char[], const char[] );

int
main()
{
    char texto[BUF_SIZE], padrao[BUF_SIZE];
    int posicao;

    printf( "Entre um texto\n" );
    fgets( texto, BUF_SIZE, stdin );

    printf( "Entre a palavra que quer procurar\n" );
    fgets( padrao, BUF_SIZE, stdin );
    padrao[strcspn( padrao, "\r\n" )] = 0;

    posicao = match( texto, padrao );

    printf( "%d\n", posicao );

    return 0;
}

int match( const char texto[], const char padrao[] )
{
    size_t i = 0;
    size_t j = 0;
    size_t tamanho_texto = 0;
    size_t tamanho_padrao = 0;
    size_t posicao = -1;

    tamanho_texto  = strlen( texto );
    tamanho_padrao = strlen( padrao );

    if ( tamanho_padrao > tamanho_texto )
    {
        return -1;
    }

    for ( i = 0 ; i <= tamanho_texto - tamanho_padrao ; ++i )
    {
        posicao = i;

        for( j = 0 ; j < tamanho_padrao ; ++j )
        {
            if ( padrao[j] != texto[i + j] )
            {
                break;
            }
        }
        if ( j == tamanho_padrao )
        {
            return posicao;
        }
    }

    return -1;
}

Or you can use the strstr function:

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

#define BUF_SIZE 100

int match( const char*, const char* );

int
main()
{
    char texto[BUF_SIZE], padrao[BUF_SIZE];
    int posicao;

    printf( "Entre um texto\n" );
    fgets( texto, BUF_SIZE, stdin );

    printf( "Entre a palavra que quer procurar\n" );
    fgets( padrao, BUF_SIZE, stdin );
    padrao[strcspn( padrao, "\r\n" )] = 0;

    posicao = match( texto, padrao );

    printf( "%d\n", posicao );

    return 0;
}

int match( const char *texto, const char *padrao )
{
    size_t tamTexto = strlen( texto );

    if( tamTexto > 0 )
    {
        char *palavra = strstr( texto, padrao );
        if( palavra != NULL )
        {
            return ( int )( palavra - texto );
        }
    }
    return -1;
}

For more information about gets() : Portuguese or English

    
22.05.2016 / 18:33
0

The strtok() function of the default library string.h is able to 'break' the sentence into words if the last token is a space.

Then, just compare each word of the sentence with the word you are looking for in a loop.

Here is a (tested) code that can do what you need:

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


int buscar_palavra( const char * frase, const char * palavra )
{
    char * f = NULL;
    char * p = NULL;
    int pos = -1;

    f = strdup( frase );

    p = strtok( f, " " );

    while( p )
    {
        if( !strcmp( palavra, p ) )
        {
            pos = p - f;
            break;
        }

        p = strtok( NULL, " " );
    }

    free(f);

    return pos;
}


int main( int argc, char * argv[] )
{
    const char * frase = "Um pequeno jabuti xereta viu dez cegonhas felizes";

    /* jabuti */
    printf( "palavra: 'jabuti' / pos: '%d'\n", buscar_palavra( frase, "jabuti" ) );

    /* xereta */
    printf( "palavra: 'xereta' / pos: '%d'\n", buscar_palavra( frase, "xereta" ) );

    /* cegonha */
    printf( "palavra: 'cegonha' / pos: '%d'\n", buscar_palavra( frase, "cegonha" ) );

    /* feliz */
    printf( "palavra: 'feliz' / pos: '%d'\n", buscar_palavra( frase, "feliz" ) );

    return 0;
}

/* fim-de-arquivo */

Output:

palavra: 'jabuti' / pos: '11'
palavra: 'xereta' / pos: '18'
palavra: 'cegonha' / pos: '-1'
palavra: 'feliz' / pos: '-1'

I hope I have helped.

    
31.05.2016 / 22:33