Cstring.h-strcmp()














































Cstring.h-strcmp()



Cstring.h-strcmp()

The built-in library function strcmp() is listed in the header file "string.h".The two strings are compared via strcmp(). Lexicographically speaking, this refers to comparing characters one at a time beginning with the first character until both strings of characters have the same length or a NULL character is met.

 

SYNTAX

The following is the syntax of strcmp() function:-

 

int strcmp(string leftStr, string rightStr );

 

PARAMETER

It takes two string as arguments and compares the string according to lexicographical order.

 

 

OUTPUT

It returns a positive value if first string is higher the lexicographical order, return zero if they are same and negative value if the first string is lower in the lexicographical order

 

 

CODE

// Use of strcmp() in C++

#include <iostream>

#include <cstring>

#include <string>

using namespace std;

 

int main()

{

 

    char s1[] = "string1";

    char s2[] = "string2";

    int c=strcmp(s1,s2);

    cout<<" \n Difference between strings are = "<<c<<endl;

    

    return 0;

}

OUTPUT


 


Comments