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>
  2.  
  3. int main() {
  4.     char str1[30], str2[30];
  5.     int i;
  6.  
  7.     printf("\nEnter two strings :");
  8.     gets(str1);
  9.     gets(str2);
  10.  
  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");
  20.  
  21.     return (0);
  22. }

Output:

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

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement

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

advertisement
advertisement
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
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.