How to compare char in c++

This C program reads the characters as input and compares the characters to find both are equal or not (greater than or smaller than).

#include<stdio.h> void main() { char ch1, ch2; printf("Enter characters: "); scanf("%c%c",&ch1, &ch2); if(ch1 == ch2) { printf("Characters are equal!"); } else if(ch1 > ch2) { printf("Character %c is greater than %c!", ch1, ch2); } else { printf("Character %c is smaller than %c!", ch1, ch2); } }

Output:

$ cc char-comparision.c $ ./a.out Enter characters: ad Character a is smaller than d! $ ./a.out Enter characters: aa Characters are equal! $ ./a.out Enter characters: db Character d is greater than b!

We can also read the character input using getchar function instead of using scanf function.

#include<stdio.h> void main() { char ch1, ch2; printf("Enter characters: "); ch1 = getchar(); ch2 = getchar(); if(ch1 == ch2) { printf("Characters are equal!"); } else if(ch1 > ch2) { printf("Character %c is greater than %c!", ch1, ch2); } else { printf("Character %c is smaller than %c!", ch1, ch2); } }

Output:

$ cc char-comparision.c $ ./a.out Enter characters: ad Character a is smaller than d! $ ./a.out Enter characters: aa Characters are equal! $ ./a.out Enter characters: db Character d is greater than b!

We ca

The strcmp() compares two strings character by character. If the strings are equal, the function returns 0.

C strcmp() Prototype

The function prototype of strcmp() is:

int strcmp (const char* str1, const char* str2);

strcmp() Parameters

The function takes two parameters:

  • str1 - a string
  • str2 - a string

Return Value from strcmp()

Return Value Remarks
0 if strings are equal
>0 if the first non-matching character in str1 is greater (in ASCII) than that of str2.
<0 if the first non-matching character in str1 is lower (in ASCII) than that of str2.

The strcmp() function is defined in the string.h header file.

Example: C strcmp() function

#include <stdio.h> #include <string.h> int main() { char str1[] = "abcd", str2[] = "abCd", str3[] = "abcd"; int result; // comparing strings str1 and str2 result = strcmp(str1, str2); printf("strcmp(str1, str2) = %d\n", result); // comparing strings str1 and str3 result = strcmp(str1, str3); printf("strcmp(str1, str3) = %d\n", result); return 0; }

Output

strcmp(str1, str2) = 1 strcmp(str1, str3) = 0

In the program,

  • strings str1 and str2 are not equal. Hence, the result is a non-zero integer.
  • strings str1 and str3 are equal. Hence, the result is 0.

In this tutorial, we’ll learn methods to compare strings in C++. Consider a scenario wherein you are required to enter your name and password to login to a particular website. In such cases, at the back end, we need to build and script functions to check and compare the input string (login details) with the string stored in the data base.

So, relating it here, in this article, will be understanding the Ways to compare string in C++ language.

Techniques to Compare Strings in C++

Strings in C++ can be compared using either of the following techniques:

  • String strcmp() function
  • In-built compare() function
  • C++ Relational Operators ( ‘==’ , ‘!=’ )

1. String strcmp() function in C++

C++ String has got in-built functions to manipulate and deal with data of string type. In order to compare two strings, we can use String’s strcmp() function.

The strcmp() function is a C library function used to compare two strings in a lexicographical manner.

Syntax:

int strcmp ( const char * str1, const char * str2 );
  • The function returns 0 if both the strings are equal or the same.
  • The input string has to be a char array of C-style string.
  • The strcmp() compares the strings in a case-sensitive form as well.

Example 1:

#include<iostream> using namespace std; #include<string.h> int main() { const char *str_inp1="JournalDEV"; const char *str_inp2="JournalDEv"; cout<<"String 1:"<<str_inp1<<endl; cout<<"String 2:"<<str_inp2<<endl; if (strcmp(str_inp1, str_inp2) == 0) cout << "\nBoth the input strings are equal." << endl; else cout << "The input strings are not equal."; }

Output:

String 1:JournalDEV String 2:JournalDEv The input strings are not equal.

Example 2:

#include<iostream> using namespace std; #include<string.h> int main() { const char *str_inp1="Python"; const char *str_inp2="Python"; cout<<"String 1:"<<str_inp1<<endl; cout<<"String 2:"<<str_inp2<<endl; if (strcmp(str_inp1, str_inp2) == 0) cout << "\nBoth the input strings are equal." << endl; else cout << "The input strings are not equal."; }

Output:

String 1:Python String 2:Python Both the input strings are equal.

2. The compare() function in C++

C++ has in-built compare() function in order to compare two strings efficiently.

The compare() function compares two strings and returns the following values according to the matching cases:

  • Returns 0, if both the strings are the same.
  • Returns <0, if the value of the character of the first string is smaller as compared to the second string input.
  • Results out to be >0, when the second string is greater in comparison.

Syntax:

int compare (const string& string-name) const;

Example 1:

#include<iostream> using namespace std; int main() { string str_inp1("Python"); string str_inp2("Python"); cout<<"String 1:"<<str_inp1<<endl; cout<<"String 2:"<<str_inp2<<endl; int res = str_inp1.compare(str_inp2); if (res == 0) cout << "\nBoth the input strings are equal." << endl; else if(res < 0) cout << "String 1 is smaller as compared to String 2\n."; else cout<<"String 1 is greater as compared to String 2\n."; }

In the above example, we have compared string 1 with string 2. As clearly seen, both the strings are the same lexicographically, it returns 0.

Output:

String 1:Python String 2:Python Both the input strings are equal.

Example 2:

#include<iostream> using namespace std; int main() { string str_inp1("Python"); string str_inp2("JournalDEV"); cout<<"String 1:"<<str_inp1<<endl; if (str_inp1.compare("Python") == 0) cout << "Strings are equal." << endl; else cout<<"Strings are not Equal\n."; cout<<"\nString 2:"<<str_inp2<<endl; if (str_inp2.compare("JournalDEv") == 0) cout << "Strings are equal." << endl; else cout<<"Strings are not Equal.\n"; }

In the above snippet of code, we have compared a string with another input string to the compare() function directly.

Output:

String 1:Python Strings are equal. String 2:JournalDEV Strings are not Equal.

3. Relational Operators in C++

C++ Relational operators such as ‘==’ and ‘!=’ can be useful in the comparison of string at an ease.

Syntax:

string1 == string 2 OR string1 != string2

Example 1: Using C++ ‘==’ operator

#include<iostream> using namespace std; int main() { string str_inp1; string str_inp2; cout<<"Enter the String 1:\n"; cin>>str_inp1; cout<<"Enter the String 2:\n"; cin>>str_inp2; if (str_inp1 == str_inp2) cout <<"Strings are equal"<<endl; else cout <<"Strings are not equal"<< endl; }

Output:

Enter the String 1: Python Enter the String 2: PythoN Strings are not equal

Example 2: Using C++ ‘!=’ operator

#include<iostream> using namespace std; int main() { string str_inp1; string str_inp2; cout<<"Enter the String 1:\n"; cin>>str_inp1; cout<<"Enter the String 2:\n"; cin>>str_inp2; if (str_inp1 != str_inp2) cout <<"Strings are not equal"<<endl; else cout <<"Strings are equal"<< endl; }

Output:

Enter the String 1: Python Enter the String 2: Python Strings are equal

Conclusion

In this article, we have understood various ways to compare strings in C++ Language.

References

  • The compare() function in C++ - Official Documentation

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.

Sign up