C Program to Find Common Parts of Two Strings


In this post, I am going to write a c program to find common parts of two strings. We will read two strings from the user. After reading the input, we will display all the common parts of these two strings.

Also Read: Switch Case in C Program to Calculate Area of Circle and Triangle

Before writing the c program, let us see the expected output first.


C program to find common parts of two strings

As you can see in the above output, we are reading two strings i.e. Sunday and Monday. These two strings have common parts i.e. nday, day, ay and y.

Also Read: C Program to Print Multiples of 5 using do-while loop

I hope you have understood the output. Now let us see the c program.

C Program to Find Common Parts of Two Strings

#include <stdio.h>
#include <stdlib.h>
int main()
    char str1[100],str2[100];
    int i,j,k,flag=0,flag1=0;
    printf("Enter any two strings\n");
        printf("Sorry, there is no COMMON Parts in given strings\n");
    return 0;

In this program, I have declared seven variables. There are five integer variables and two-character arrays.

  1. The str1 and str2 are two character arrays. We will store our two strings in these two variables.
  2. We have to read and compare each and every character. For this, we are going to use two for loops and three loop variables i.e. i, j and k. Actually, there are only two loop variables but we will initialise the value of i to k.
  3. The variables flag and flag1 are status variables. Initial values of these two are variables is set to zero. When we get atleast a single substring then we will set the values of these variables to 1. On the basis of these values, we can avoid unwanted blank space and display the message if there is no substring.

As I said above, we will compare each and every character of both strings. I hope you have understood this program. If you have any difficulty, please feel free to contact me.

Thank you.

Some Important C Programs

  1. Program in C to Find Longest Line in a File
  2. Palindrome in C using Pointers
  3. Insert and Delete element in Array in C using switch case
  4. C Program to Add Alternate Elements of a 2D Array
  5. Arrays in C for Complete Beginners
  6. C Program to Find Area of a Circle using Preprocessor
  7. Program in C to Remove White Spaces and Comments from a File
  8. C Program to Print Numbers Except Multiples of n
  9. Reverse a Number using getchar and putchar function in c
  10. The while loop in C Programming

Leave a Comment