Software Developer
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, a comparison can be made with the initialization of any number of strings with char array and comparisons.
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 significance.
Representation of various types of syntax for strcmp () or compare () function:
All the different types of syntaxes will be discussed with one example each to get the insight and output for clarification.
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}