strcmp in c++ code example
Example 1: c++ compare char
// syntax
#include <cstring> // this needs to be at the top of the script/code
std::strcmp(<1st-char>,<2nd-char>)
// example (assuming: char_1 = 'Compare me'; char_2 = 'Compare_me')
#include <cstring>
if (std::strcmp(char_1,char_2) == 0) {
std::cout << "The char's that you compared match!" << std::endl;
}
else {
std::cout << "The char's that you compared DON'T match" << std::endl;
}
// OUTPUT: The char's that you compared match!
/*
NOTE: the following outputs of std::strcmp indicate:
[less than zero] : left-hand-side appears before right-hand-side in lexicographical order
[zero] : the chars are equal
[greater than zero] : left-hand-side appears after right-hand-side in lexicographical order
*/
Example 2: strcmp c++
#include<stdio.h>
#include<string.h>
int main()
{
char char1[] = "coucou";
char char2[] = "coucou";
if( strcmp(char1, char2) == 0 )
printf("Strings are the same");
else
prinf("Strings are differentes");
return 0;
}
Example 3: strcmp c++
int strcmp ( const char * str1, const char * str2 );
// returning value | indicates
// <0 the first character that does not match has a lower value in ptr1 than in ptr2
// 0 the contents of both strings are equal
// >0 the first character that does not match has a greater value in ptr1 than in ptr2