Updated March 21, 2023
Introduction to strcmp() in C++
The following article sets out the outline for strcmp () in C++. There are many string functions and structures available in any programming language so as in C++. It includes functions present in cstring header file. It works in a way that a string that is used for manipulation gets stored in a predefined c-style char array and is used to copy it in a way that that the character gets stored in char array and then a comparison is made between the characters of two strings. Moreover, comparison can be made with the initialization of any number of strings with char array and comparisons.
Syntax
Following is a syntax:
int strcmp (const char * str1, const char * str2);
This syntax represents that str 1 and str 2 are the two strings as parameters inside the function. This will compare both the arguments i.e. both the strings and then it will return the compared result in the form of int value which in turn has its own significance.
- If the int value returned after string comparison is 0 then it signifies that characters in both the string are the same.
- If the int value returned is 1 i.e. when the character is not matching in the first string have an ASCII value which is larger than the character present in the same index of the second string.
- If the returned int value has is -1 then the character which is not matching the first string and consists of an ascii value which is less than the character at the same position or index of the second string.
strcmp possess two things in it one string which can be any char array string and as many numbers as possible and a compare function in it. In generalization with terms of programming languages, we have a compare () function which is a public member function of string class and string.h header file. The compare function compares the value of string object (or a substring) to the sequence of characters specified in its arguments.int in the above syntax is the final compared string return type and the return type should be string only it should not be Boolean or any other form. There are many more ways of defining string compare function which again depends on the type of strings that will be used for comparison and the requirement or the need of the hour.
Representation of various types of syntax for strcmp () or compare () function:
- int string::compare (const string& str) const
- int string::compare (size_type idx, size_type len, const string& str) const
- int string::compare (size_type idx, size_type len, const string& str, size_type str_idx, size_type str_len) const
- int string::compare (const char* cstr) const
- int string::compare (size_type idx, size_type len, const char* cstr) const
- int string::compare (size_type idx, size_type len, const char* chars, size_type chars_len) const
Examples to Implement strcmp () in C++
All the different types of syntaxes will be discussed with one example each to get the insight and output for clarification.
Example #1
#include <iostream>
#include <cstring>
using namespace std;
int main ()
{
char frst [20] = "Programming";
char sec [20] = "Programming";
char thrd [20] = "Programming";
cout<<"Content of frst string: " <<frst <<"\n";
cout<<"Content of sec string: " <<sec <<"\n";
int result = strcmp (frst, sec);
cout<<" String comparison between " <<frst <<" and " <<sec<<" : " <<result <<"\n";
result = strcmp(sec, thrd);
cout<<" String comparison between " <<sec <<" and " <<thrd<<" : " <<result <<"\n";
return 0;
}
Output:
Example #2
#include <iostream>
using namespace std;
void comprOprtion(string s1, string s2)
{
if((s1.compare(s2)) < 0)
cout << s1 << "is less than " << s2 << endl;
if((s1.compare(s1)) == 0)
cout << s1 << " is same as " << s1 << endl;
else
cout << "Strings are not matching ";
}
int main ()
{
string s1("God");
string s2("looks after everyone");
comprOprtion(s1, s2);
return 0;
}
Output:
Example #3
#include <iostream>
using namespace std;
void cmprOprtion(string s1, string s2)
{
if ((s2.compare(8, 9, s1)) == 0)
cout << "sphere, "<< s1 << " have " << s2;
else
cout << "String is not matching";
}
int main ()
{
string s1("Grapes");
string s2("areverysourandsweet");
cmprOprtion (s1, s2);
return 0;
}
Output:
Example #4
#include <iostream>
using namespace std;
void comprOprtion (string s1, string s2)
{
if ((s1.compare(1, 6, s2, 2, 4)) == 0)
cout << "Welcome to " << s1 << s2 << " Programming";
else
cout << "Strings not matching with programming ";
}
int main ()
{
string s1("Programs");
string s2("arenotprogrmming skills");
comprOprtion (s1, s2);
return 0;
}
Output:
Example #5
#include <iostream>
using namespace std;
void comprOprtion (string s1)
{
if ((s1.compare(0, 3, "educba")) == 0)
cout << s1 << " are " << "just kind people";
else
cout << "Strings are not at all matching ";
}
int main ()
{
string s1("educba");
comprOprtion(s1);
return 0;
}
Output:
Example #6
#include <iostream>
using namespace std;
void comprOpration (string s1, string s2)
{
if ((s1.compare(0, 6, "technocrats", 5)) == 0)
cout << "This is " << s1 << s2 ;
else
cout << "Strings are not matching ";
}
int main ()
{
string s1("technocrats");
string s2("becomesbig giant");
comprOpration (s1, s2);
return 0;
}
Output:
Example #7
#include<iostream>
#include<cstring>
using namespace std;
int main ()
{
char one [20] = "red color is dark";
char secnd [20] = "rose is red";
cout<< "The content of one string : " << one << "\n";
cout<< "The content of secnd string : " << secnd << "\n";
int result = strcmp(one, secnd);
cout<< "String comparison between " << one << " and " << secnd << " : " << result;
return 0;
}
Output:
Conclusion – strcmp() in C++
String compare is also one form of predefined inbuild functions of string.h header file as part of the string class. There are many forms and syntaxes to represent the same, but the main advantage is to compare an array of char in any form of a string that can be used internally for comparison of characters up to null characters. Compare functions play a great role in comparing all the types of strings and composition and decomposition but the basic important thing that returns type should be integer type only.
Recommended Articles
This has been a guide to strcmp() in C++. Here we discuss the introduction, syntax, and different examples of strcmp() in C++. You may also have a look at the following articles to learn more –