C++ Program to Compare Two Strings Using Pointers: We are comparing two string with the help of pointers, without using strcmp() function. A user-defined function str_cmp() is created which takes two character pointers as an argument.
C++ Program to Compare Two Strings Using Pointers
#include <iostream>
using namespace std;
bool compare(char *str1, char *str2)
{
while (*str1 == *str2)
{
if (*str1 == '\0' && *str2 == '\0')
return true;
str1++;
str2++;
}
return false;
}
int main()
{
char str1[] = "CodeBlah.com";
char str2[] = "CodeBlah.com";
if (compare(str1, str2) == 1)
cout << str1 << " " << str2 << " are Equal";
else
cout << str1 << " " << str2 << " are not Equal";
}
Output of Program
CodeBlah.com CodeBlah.com are Equal