

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Comparing String objects using Relational Operators in C++
Here we will see how to compare two strings in C++. The C++ has string class. It also has the compare() function in the standard library to compare strings. But here we will use the conditional operators like ==, !=, <, >, <=, >=. These operators check the strings character by characters. Let us see the code to get better idea.
Example
#include<iostream> using namespace std; void compareStrings(string s1, string s2) { if (s1 != s2) cout << s1 << " is not equal to "<< s2 << endl; else if(s1 == s2) cout << "Strings are equal"; if (s1 > s2) cout << s1 << " is greater than "<< s2 << endl; else if(s1 < s2) cout << s2 << " is greater than "<< s1 << endl; } int main() { string s1("hello"); string s2("helLo"); compareStrings(s1, s2); }
Output
hello is not equal to helLo hello is greater than helLo
- Related Questions & Answers
- Java Relational Operators
- Relational Operators in C++
- PHP Comparing Objects
- Relational Set Operators in DBMS
- Basic Operators in Relational Algebra
- Relational Operators in Dart Programming
- C++ Relational and Equality Operators
- Relational and comparison operators in C++
- What are relational operators in C#?
- Relational and Logical Operators in C
- Extended Operators in Relational Algebra in C++
- What are the relational operators in Java?
- Relational Operators on STL Array in C++
- What are relational operators in C language?
- Find maximum in an array without using Relational Operators in C++
Advertisements