0

Check if two char arrays are equals in C++

 1 year ago
source link: https://thispointer.com/check-if-two-char-arrays-are-equals/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Check if two char arrays are equals in C++ – thisPointer

This tutorial will discuss about a unique way to check if two char arrays are equals.

We can use the strcmp() function to compare two char arrays. It accepts two strings (char pointers) as arguments, and compares both the strings alphabetically. If both the strings are equal then it will return 0.

So, to check if two char arrays are equal or not, we need to pass both the char arrays into the strcmp() function. If it returns 0, then it means both the char arrays or strings are equal.

Let’s see the complete example,

#include <iostream>
#include <string.h>
int main()
char firstArr[10] = "sample";
char secondArr[10]= "sample";
// Check if two char arrays are equal
if(strcmp(firstArr, secondArr) == 0)
std::cout<<"Both Char Arrays are equal \n";
std::cout<<"Both Char Arrays are not equal \n";
return 0;
#include <iostream>
#include <string.h>

int main()
{
    char firstArr[10] = "sample";
    char secondArr[10]= "sample";

    // Check if two char arrays are equal
    if(strcmp(firstArr, secondArr) == 0)
    {
        std::cout<<"Both Char Arrays are equal \n";
    }
    else
    {
        std::cout<<"Both Char Arrays are not equal \n";
    }
    return 0;
}

Advertisements

Output :

Both Char Arrays are equal
Both Char Arrays are equal

Summary

Today we learned about several ways to check if two char arrays are equals. Thanks.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK