Using strcmp and strncmp : string function « Data Types « C++ Tutorial






#include <iostream>
using std::cout;
using std::endl;

#include <iomanip>
using std::setw;

#include <cstring>
using std::strcmp;                                     
using std::strncmp;                                     

int main()
{
   char *s1 = "abc";
   char *s2 = "abC";
   char *s3 = "Abc";
 
   cout << "s1 = " << s1 << "\ns2 = " << s2 << "\ns3 = " << s3 
      << "\n\nstrcmp(s1, s2) = " << strcmp( s1, s2 ) 
      << "\nstrcmp(s1, s3) = " << strcmp( s1, s3 ) 
      << "\nstrcmp(s3, s1) = " << strcmp( s3, s1 );

   cout << "\n\nstrncmp(s1, s3, 6) = "
      << strncmp( s1, s3, 2 ) << "\nstrncmp(s1, s3, 7) = " 
      << strncmp( s1, s3, 2 ) << "\nstrncmp(s3, s1, 7) = " 
      << strncmp( s3, s1, 5 ) << endl;
   return 0;
}
s1 = abc
s2 = abC
s3 = Abc

strcmp(s1, s2) = 1
strcmp(s1, s3) = 1
strcmp(s3, s1) = -1

strncmp(s1, s3, 6) = 1
strncmp(s1, s3, 7) = 1
strncmp(s3, s1, 7) = -1








2.24.string function
2.24.1.Using strcpy()
2.24.2.Using strncpy()
2.24.3.strcpy, strlen, strcmp, strcat
2.24.4.Convert a string to uppercase.
2.24.5.Using strcat and strncat.
2.24.6.Using strcmp and strncmp
2.24.7.Using strtok
2.24.8.Using strlen