C program to compare two string.

Posted by Mangesh on April 11, 2018

/* c program to compare to two string. */

Description :

We shall first accept two string. Then check the length of both string if length is not equal we shall declare that string is not equal else we shall compare both string character by character. If all character are equal then both string is equal else both string is not equal.

Program :

void main()
  int i=0;
  char str1[20],str2[20];
  printf("Enter the first string : ");
  printf("Enter the second string : ");
  if(strlen(str1) == strlen(str2))
    while(str1[i] == str2[i] && i < strlen(str1))
  if(strlen(str1) == i)
    printf("both string are equal.");
    printf("both string are not equal.");

Output :

c program for strcmp Executed and tested in Turbo C 3.2

Written with from Mangesh.

Related Post
1 C program for naive string matching algorithm.
2 C program to check whether the given string is palindrome or not.
3 C program to concatenate two string.
4 C program to check whether two strings are anagram or not.
5 C program to reverse the string.
6 C program to replace a character in a string.
7 C program to compare two string.
8 C program to compare two string using strcmp function.
Latest Post
1 C program to implement Queue using linked list.
2 C program for binary search tree (BST).
3 C program to search an element in linked list.
4 C program for postorder traversal in binary tree.
5 C program for preorder traversal in binary tree.