www.ruttuali.com
How to check whether an array contains a word in C language/*
Example: check if the word is in the array in C language
*/
#include <stdio.h>
int main()
{
char c_to_search[5] = "hello";
char text[68] = "hi, this is lautturi.com. hello world";
int pos_search = 0;
int pos_text = 0;
int len_search = 4;
int len_text = 67;
for (pos_text = 0; pos_text < len_text - len_search;++pos_text)
{
if(text[pos_text] == c_to_search[pos_search])
{
++pos_search;
if(pos_search == len_search)
{
// The word was found in the array
printf("match from %d to %d\n",pos_text-len_search,pos_text);
return;
}
}
else
{
pos_text -=pos_search;
pos_search = 0;
}
}
// The word was not found
printf("no match\n");
return 0;
}