Keyword in String C language

Asked

Viewed 719 times

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 not finding returning -1. I’m having trouble with substring, type my program identifies bits of words, how to fix it?

#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;
}
  • Make sure you don’t have buffer overflow. None of your inputs can be more than 99 characters long.

2 answers

1

The function strtok() of the standard library string.h is able to 'break' the phrase into words if the token passed is a space.

Then, just compare each word of the phrase with the word you’re searching for in a loop.

Follow a code (tested) capable of doing 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 */

Exit:

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

I hope I’ve helped.

0

Do not use the function gets() because the function does not receive the size of the buffer and a buffer overflow can occur. I used fgets and your program worked, only 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 function strstr:

#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 on the gets(): Português or English

  • fought by the help, but still the result is the same, if I type the following phrase: "program and cool" and try to search for the following string : "mar" the result should be -1 , but the program understands "sea" as a piece of "program" and returns me its position, when actually it shouldn’t happen, you should just find whole words, not pieces of them

  • @Rodrigosilva the function strtok() of string.h can solve your problem. Check my answer. ;)

Browser other questions tagged

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