Implement strcmp () in C++

Implement strcmp () in C++
Techiio-author
Written by Sagar RabidasFebruary 11, 2022
10 min read
C++
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Techiio-author
Sagar Rabidas

Software Developer

In this blog, we will discuss Implementing strcmp () in C++

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, a 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 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 has an ASCII value that 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 does not match the first string and consists of an ASCII value that is less than the character at the same position or index of the second string.
  • strcmp possesses 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 a 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;
}

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;
}

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;
}

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;
}

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;
}

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;
}

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;
}

C
C++
strcmp ()
strcmp () in C++
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance