C Program to Perform String Matching Using String Library

This is a C Program to perform string matching using String Library. A text and a pattern is given as input. The pattern is searched for in the text and all instances of the pattern are given as output.

Here is source code of the C Program to Perform String Matching Using String Library. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include<stdio.h>
  3. int main() {
  4.     char str1[30], str2[30];
  5.     int i;
  7.     printf("\nEnter two strings :");
  8.     gets(str1);
  9.     gets(str2);
  11.     i = 0;
  12.     while (str1[i] == str2[i] && str1[i] != '\0')
  13.         i++;
  14.     if (str1[i] > str2[i])
  15.         printf("str1 > str2");
  16.     else if (str1[i] < str2[i])
  17.         printf("str1 < str2");
  18.     else
  19.         printf("str1 = str2");
  21.     return (0);
  22. }


$ gcc StringMatchingUsingStringLib.c
$ ./a.out
Enter two strings :
str1 < str2

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.