TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C++ String compare() function

C++ String compare function tutorial for beginners and professionals with examples on constructor, if-else, switch, break, continue, comments, arrays, object and class, exception, static, structs, inheritance, aggregation etc.

<< Back to CPP

C++ String compare()

This function compares the value of the string object to the sequence of characters specified by its parameter.

Syntax :

Suppose str1 and str2 are two strings and we want to compare these two strings then its syntax would look like:

int k= str1.compare(str2);
  • k==0 : If k contains value zero, it means both the strings are equal.
  • k!=0 : If k does contain value zero, it means both the strings are unequal.
  • k>0 : If k contains value more than zero, either the value of the first character is greater in the compared string or all the compared characters match but the compared string is longer.
  • k<0 : If k contains value less than zero, either the value of the first character is lower in the compared string or all the compared characters match but the compared string is shorter.

Example 1

#include<iostream>
using namespace std;
void main()
{
   string str1="Hello";
   string str2="TheDeveloperBlog";
    int k= str1.compare(str2);
    if(k==0)
         cout<<"Both the strings are equal";
     else
       cout<<"Both the strings are unequal";

}

Output:

Both the strings are unequal

Consider two strings str1 and str2. str1 contains value 'Hello' and str2 contains value 'TheDeveloperBlog' ,We compare these two strings using compare method and this compare method always returns integer value.When we compare these two strings we get the value less than zero.Now,in this case 'if' condition fails then else statement will run and will print "Both the strings are unequal".

Example 2

#include<iostream>
using namespace std;
void main()
{
   string str1="Welcome to TheDeveloperBlog";
   string str2="Welcome to TheDeveloperBlog";
   int i=str1.compare(str2);
   if(i==0)
  cout<<"strings are equal";
else
cout<<"strings are not equal";
}

Output:

Strings are equal

Next TopicC++ Strings




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf