03/03/2023

C Program to Compare Two Strings Without Using Strcmp Function

Write a C Program to Compare Two Strings Without Using Strcmp Function or How to compare two strings in c without using the strcmp Function or Program to Compare Two Strings without using strcmp() or string compare in c without using strcmp. There are two methods one is using an Strcmp library function second is Without Using Strcmp Function. Here without using an strcmp() function.

C Program to Compare Two Strings Without Using Strcmp Function

We first calculate the size of both strings and if the size of both strings is not equal then the program prints the message "Both strings are not equal" if the size of both stings is equal then the program compares Two Strings character by character and return if both strings are equal or not according to the string analysis. 

Below is both method for comparing a string using library function or without using library functions. compare two strings without using the strcmp code below.

Using the Strcmp Library Function

strcmp(string1 ,string2)

Compare Two Strings Without Using Strcmp Function


for (i = 0, j = 0; str1[i] != '\0', str2[j] != '\0'; i++, j++)
{
	if (str1[i] == str2[j])
	{
		flag = 1;
	}
	else
	{
		flag = 0;
	}
}

This condition is used when the size of two strings is equal and we have to compare both string character by character.

Sponsor: Install and access your important work applications and software no matter where you are from any device ( PC / Android / iOS ) with a cloud desktop from www.CloudDesktopOnline.com. For cloud-related business software such as SharePoint, and Office365, try Apps4Rent.

Read: C Program to Compare Two Strings Using Pointers

C Program to Compare Two Strings Without Using Strcmp


#include <bits/stdc++.h>
using namespace std;
int main()
{
	/*C Program to Compare Two Strings Without Using Strcmp*/

	cout << "=====================================";
	cout << "\nVisit - www.programmingwithbasics.com";
	cout << "\n=====================================";

	char str1[20], str2[20], i = 0, j = 0, flag = 0;

	cout << "\n\nEnter First String : \n";
	gets(str1);

	cout << "Enter Second String : \n";
	gets(str2);

	while (str1[i] != '\0')
	{
		i++;
	}

	while (str2[j] != '\0')
	{
		j++;
	}

	if (i != j)
	{
		flag = 0;
	}
	else
	{
		for (i = 0, j = 0; str1[i] != '\0', str2[j] != '\0'; i++, j++)
		{
			if (str1[i] == str2[j])
			{
				flag = 1;
			}
			else
			{
				flag = 0;
			}
		}
	}

	if (flag == 0)
	{
		cout << "Both Strings Are Not Equal\n";
	}
	else
	{
		cout << "Both Strings Are Equal\n";
	}

	return 0;
}

Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

3 comments:

  1. for(i=0,j=0;str1[i]!='\0',str2[j]!='\0';i++,j++)
    {
    if(str1[i]==str2[j])

    flag=1;
    else
    flag=0 //There should be a else part...otherwise it fails to compare between hello &
    //helli
    }

    ReplyDelete
    Replies
    1. Thank you Very Much Nick Moni for notifying Errors. Now Program Is Updated


      Keep Sharing and Keep visiting

      Delete
  2. For instance :
    Abcde

    abcde
    -> 1 ( following your code )
    In fact, it was 0.

    ReplyDelete