How can I separate a char vector?

0

Hello I need to do as a search engine where the user enters a word "caramel" and then enters the letter "the" should print the words you are looking for "candy"

The problem is that the user must enter "caramel" and I do not know how to do that.

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

int main(){

    char cadena[30];
    char subCadena[15];
    int i = 0;

        printf("Bienvenido \n\n");

        printf("Introduce una palabra: >>\n");
            fgets(cadena, sizeof(cadena), stdin);

        printf("\nIntroduce las letras que recuerdas:>>\n");
            fgets(subCadena, sizeof(subCadena), stdin);

        cadena[strlen(cadena) -1] = '
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>

int main(){

    char cadena[30];
    char subCadena[15];
    int i = 0;

        printf("Bienvenido \n\n");

        printf("Introduce una palabra: >>\n");
            fgets(cadena, sizeof(cadena), stdin);

        printf("\nIntroduce las letras que recuerdas:>>\n");
            fgets(subCadena, sizeof(subCadena), stdin);

        cadena[strlen(cadena) -1] = '%pre%';
        subCadena[strlen(subCadena) -1] = '%pre%';

        for(i; (cadena[i] = toupper(cadena[i])); i++);
        for(i = 0; (subCadena[i] = toupper(subCadena[i])); i++);

        if(strstr(cadena, subCadena) != NULL){
        printf("\n\nla palabras que buscas es %s \n\n", cadena);
        }
       else{

        printf("\n\n no se encuentra \n\n");
            }
        return 0;

}
'; subCadena[strlen(subCadena) -1] = '%pre%'; for(i; (cadena[i] = toupper(cadena[i])); i++); for(i = 0; (subCadena[i] = toupper(subCadena[i])); i++); if(strstr(cadena, subCadena) != NULL){ printf("\n\nla palabras que buscas es %s \n\n", cadena); } else{ printf("\n\n no se encuentra \n\n"); } return 0; }
    
asked by Noemi Pavon 16.01.2017 в 14:10
source

1 answer

2

To do what you want you can resort to two functions:

  • strtok() : This function divides a string based on given delimiters.
  • strstr() : This function locates one substring within another.

If you put them to work together you can get something like this:

char buffer[100];
strcpy(buffer,"caramelo|el");
char* palabra = strtok(buffer,"|");
char* fragmento = strtok(NULL,"|");

if( strstr(palabra,fragmento) != NULL )
  puts("Fragmento encontrado\n");
else
  puts("No hay coincidencias\n");

The function strtok is used to separate the word from the fragment to be searched, while with strstr we find out if the fragment is in the initial word or not.

PD .: Both functions are in the library string.h

    
answered by 16.01.2017 / 15:55
source