string compare() in C++

compare(): It compares to a string. This functions returns Integer <0, 0, or > 0 depending on

the value of the string to be compared.

It returns an integer < 0 if this string is ordered before  __str.

It returns 0 if its values are equivalent.

It returns  > 0 if this string is ordered after __str. 

Parameters: 

__str – String to compare against.

Syntax:

str.compare(__str)

For Example:

str  ="abc"

__str = "aa"

str.compare(__str) = > It returns 1 because the string str is comes after string __str.

Approach

C++

#include <bits/stdc++.h>
using namespace std;

int main()
{
    string str = "abc";
    string str2 = "aa";

    cout << str.compare(str2<< "\n";
    

    return 0;
}

No comments:

Post a Comment