Answers for "search for character in an array in c"

C
0

how to check the word is present in given char array in c

#include <stdio.h>
int main()
{
    char c_to_search[5] = "asdf";

    char text[68] = "hello my name is  there is some other string behind it n asdf";

    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)
            {
                // match
                printf("match from %d to %dn",pos_text-len_search,pos_text);
                return;
            }
        }
        else
        {
           pos_text -=pos_search;
           pos_search = 0;
        }
    }
    // no match
    printf("no matchn");
   return 0;
}
Posted by: Guest on November-04-2020

Code answers related to "search for character in an array in c"

Code answers related to "C"

Browse Popular Code Answers by Language