Section 4.1 - Find the pattern in the line and print it#

Program#

#include <stdio.h>
#define MAXLINE 1000 /* maximum input line length */
int mgetline(char line[], int max);
int strindex(char source[], char searchfor[]);
char pattern[] = "ould"; /* pattern to search for */
/* find all lines matching pattern */
int main()
{
    char line[MAXLINE];
    int found = 0;
    while (mgetline(line, MAXLINE) > 0)
        if (strindex(line, pattern) >= 0) {
            printf("%s", line);
            found++;
        }
        return found;
}

/* getline: get line into s, return length */
int mgetline(char s[], int lim)
{
    int c, i;
    i = 0;
    while (--lim > 0 && (c=getchar()) != EOF && c != '\n')
        s[i++] = c;
    if (c == '\n')
        s[i++] = c;
    s[i] = '\0';
    return i;
}

/* strindex: return index of t in s, -1 if none */
int strindex(char s[], char t[])
{
    int i, j, k;
    for (i = 0; s[i] != '\0'; i++) {
        for (j=i, k=0; t[k]!='\0' && s[j]==t[k]; j++, k++)
            ;
        if (k > 0 && t[k] == '\0')
            return i;
    }
    return -1;
}

Explanation#

This program searches particular pattern in a given string. As per the program we are going to search for the pattern ould and print the line which has the same.

Let us say that we give the input as:

This line would print
This line will not print

The output would be:

This line would print

As it contains the pattern ould. The curx of the program is in the strindex function.

int strindex(char s[], char t[])
{
    int i, j, k;
    for (i = 0; s[i] != '\0'; i++) {
        for (j=i, k=0; t[k]!='\0' && s[j]==t[k]; j++, k++)
            ;
        if (k > 0 && t[k] == '\0')
            return i;
    }
    return -1;
 }

Here we have the source string in s and target string in t. We start taking each character in s and starting at the position of the character, we check if the entire target string t is present in the string.

The checking for the entire target is present is done by the second for loop and the if statement.:

for(j=i, k=0; t[k]!='\0' && s[j]==t[k]; j++, k++)
    ;
if (k > 0 && t[k] == '\0')
    return i;

In the first for loop we use a temperorary variable k to iterate through and check if the target string t is present in s. If the target string is entirely present, which is ensured by if statement checking for 0, we return the position i.

In the main program, if we find the position greater than 0, we print the line.