C++ Program to Compare Two Strings

Write C++ Program to Compare Two Strings

// CPP Program to Compare Two Strings

#include <iostream>
#include <string.h>

using namespace std;

int main()
{
	char S1[] = "CodeCrucks",S2[] = "CodeCrucks";
	int i = 0,j = 0,flag = 0;

	// Using inbuilt function
//	if(strcmp(S1,S2)==0)
//	 	cout << "Strings are equal";
//	else
//	 	cout << "Strings are not equal";


	while(S1[i] != '\0' && S2[i] != '\0')
	{
	    if(S1[i] != S2[i])
		{
            flag = 1;
			break;
        }
		i++;
	}

	cout << "String 1 :--> " << S1;
	cout << "\nString 2 :--> " << S2;

	if(flag == 0)
		cout << "\n\nStrings are equal ";
	else
		cout << "\n\nStrings are not equal ";

	return 0;
}

Output:

Run 1:
String 1 :--> Code
String 2 :--> Crucks

Strings are not equal

Run 2:
String 1 :--> CodeCrucks
String 2 :--> CodeCrucks

Strings are equal

≪ Previous | Next ≫