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 :

#include<stdio.h>
#include<conio.h>
void main()
{ 
  int i=0;
  char str1[20],str2[20];
  clrscr();
  printf("Enter the first string : ");
  scanf("%s",str1);
  printf("Enter the second string : ");
  scanf("%s",str2);
  if(strlen(str1) == strlen(str2))
  {
    while(str1[i] == str2[i] && i < strlen(str1))
    {
      i++;
    }
  }
  if(strlen(str1) == i)
  {
    printf("both string are equal.");
  }
  else
  {
    printf("both string are not equal.");
  }
  getch();
}

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.