Program to compare two string without using strcmp

2 years ago Admin 1

Compare two string without using strcmp

This article illustrates how to compare two string without using strcmp in c/c++.

compare two string without using strcmp

Here is the source code and output of both the programs.

C Programming language

Program


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char first[100],second[100];
int flag=0;
clrscr();
printf("Enter first string:- ");
gets(first);
printf("Enter second string:- ");
gets(second);

for(i=0;first[i]!='\0';i++)
{

if(first[i]==second[i])
 {
  flag=1;
 }
 else
 {
  flag=0;
 }
}
if(flag==1)
{
 printf("Entered string is equal");
}
else
{
printf("Entered string is not equal");
}
getch();
}

After successfully compiling and executing the above C program,output is:-

compare two string without using strcmp


C++ Programming language

Program


#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char first[100],second[100];
int flag=0;
cout<<"Enter first string:- ";
gets(first);
cout<<"Enter second string:- ";
gets(second);

for(i=0;first[i]!='\0';i++)
{
if(first[i]==second[i])
 {
  flag=1;
 }
 else
 {
  flag=0;
}
}
if(flag==1)
{
 cout<<"Entered string is equal";
}
else
{
cout<<"Entered string is not equal";
}
getch();
}

After successfully compiling and executing the above C++ program,output is:-

compare two string without using strcmp


Comment below if you are facing any problem or you find any error in the above program and also subscribe to our newsletter for more updates.


Read more c/c++ Programs published by snapcout:-

Program to concatenate two strings using strcat
Program to concatenate two strings without using strcat
Program to copy one string into another using strcpy
Program to copy string without using strcpy()
Program to Convert String into Uppercase Using strupr()


About article

Written By:- Admin

Published on:- 04/10/2017

Published By:- Snapcout


Don't forget to share the article if you like it:-