Comparing Strings with strcmp and strncmp - C++ Data Type

C++ examples for Data Type:char array

Description

Comparing Strings with strcmp and strncmp

Demo Code

#include <iostream> 
#include <iomanip> 
#include <cstring> // prototypes for strcmp and strncmp 
using namespace std; 

int main() //from www.  j  av  a  2  s  . c  o  m
{ 
    char *s1 = "Happy New Year"; 
    char *s2 = "Happy New Year"; 
    char *s3 = "Happy Holidays"; 

    cout << "s1 = " << s1 << "\ns2 = " << s2 << "\ns3 = " << s3 
        << "\n\nstrcmp(s1, s2) = " << setw( 2 ) << strcmp( s1, s2 ) 
        << "\nstrcmp(s1, s3) = " << setw( 2 ) << strcmp( s1, s3 ) 
        << "\nstrcmp(s3, s1) = " << setw( 2 ) << strcmp( s3, s1 ); 

    cout << "\n\nstrncmp(s1, s3, 6) = " << setw( 2 ) 
        << strncmp( s1, s3, 6 ) << "\nstrncmp(s1, s3, 7) = " << setw( 2 ) 
        << strncmp( s1, s3, 7 ) << "\nstrncmp(s3, s1, 7) = " << setw( 2 ) 
        << strncmp( s3, s1, 7 ) << endl; 
}

Result


Related Tutorials