verify the existence of character by character of a string with a paragraph in C


How can I store a list of words and then when entering a paragraph by keyboard the program can verify if that paragraph (entered by keyboard) contains the letters of the words stored previously.


stored words:

sol, luz, ...

by keyboard:

"hola soy programador"

word sun

existe la S, existe la O y existe la L dentro del párrafo.

and so on with the other stored words.

I hope to have explained correctly for the moment I thought about alamcenar the words in vectors but I do not know if it is correct and many thanks in advance.


int main(){
   char pal1[2][10]= {"sol","agua,""luz"};

   char nombre[100]; 
   printf("Escribe una frase: "); 
   fgets(nombre, 100, stdin);

  return 0; 
asked by Jose N 22.07.2018 в 03:57

2 answers


If I understood you well, you only need to know if all the letters of a word exist in a sentence; if so, the code is quite simple:

unsigned existe(const char *palabra, const char *frase)
    unsigned resultado = 0u;
    unsigned letras = strlen(palabra);

    for (const char *buscar = palabra; *buscar; ++buscar)
        unsigned encontrada = 0;

        for (const char *en = frase; *en && !encontrada; ++en)
            encontrada = *buscar == *en ? 1 : 0;

        resultado += encontrada;

    return (resultado == letras) ? 1 : 0;

The function existe returns 1 if all the letters of palabra are present in frase and 0 otherwise. To do this, it goes from letter to letter of palabra and compares them with all the letters of frase , for each match for the loop and adds 1 to the counter resultado , if at the end of the function this counter is equal to the length of the received word: then all the letters are present in the sentence:

int main()
    char pal1[3][10] = {"sol", "agua", "luz"};
    char nombre[100] = {};
    printf("Escribe una frase: "); 
    fgets(nombre, 100, stdin);

    for (int i = 0; i != 3; ++i)
        printf("'%s' existe en '%s'? %d\n", pal1[i], nombre, existe(pal1[i], nombre));

    return 0;

The previous code, if you enter the phrase hello I am a programmer , shows the following output:

'sol' existe en 'hola soy un programador'? 1
'agua' existe en 'hola soy un programador'? 1
'luz' existe en 'hola soy un programador'? 0

Keep in mind that the proposed algorithm does not take into account repetition of letters, that is: "amalgama" would count as existing in "hola soy un programador" even though the word has four 'a' and the phrase has only three.

answered by 25.07.2018 в 10:48

I leave the code of the function. In this case, use the strchr method of string.h, which searches for a character within a string, returning the pointer of the first occurrence.

The word string will be traversed as long as the pointer ptr is different from NULL and is not the end of the string (this would be when word [i] is equal to '\ 0'). In case there is a character that does not exist in the sentence, the cycle will end and the result res would be 0 (false), otherwise, res will be 1 (true).

int existe(const char* frase, const char* palabra){
int i=0,res=1;
char* ptr=strchr(frase,palabra[i]);
while(ptr!=NULL&& palabra[i]){
return res;


I hope it serves you. Any questions you have, do not hesitate to tell me.

answered by 30.07.2018 в 03:13