Search
 
SCRIPT & CODE EXAMPLE
 

C

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 
 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 %d
",pos_text-len_search,pos_text);
                return;
            }
        }
        else
        {
           pos_text -=pos_search;
           pos_search = 0;
        }
    }
    // no match
    printf("no match
");
   return 0;
}
Comment

PREVIOUS NEXT
Code Example
C :: c pause for 1 second 
C :: prime numbers 
C :: insert image material ui 
C :: Relational Operator in C language 
C :: working outside of application context 
C :: fwrite c 
C :: c unused variable 
C :: %= in c 
C :: static variable c 
C :: pointer in c 
C :: how to declare a struct in c 
C :: c convert float to int 
C :: ansi c read write bmp 
C :: setw in c 
C :: Here is a program in C that illustrates the use of fprintf() to write a text file: 
C :: c program boilerplate 
C :: main prototype 
C :: gdebi install with yes option 
C :: creation of a thread 
C :: code to reverse the words in a sentnce 
C :: Handling exceptions during datetime conversion 
C :: assembly lea instruction 
C :: c byte vs char 
C :: Single-line Comments in C 
C :: arr+1 vs &arr+1 
C :: string compare in c 
C :: how to write 2d array from bin file in c 
C :: Sampoo C programming 
C :: C Increment and Decrement Operators 
C :: visual studio code 
ADD CONTENT
Topic
Content
Source link
Name
5+6 =