
- 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 two strings 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. This function checks the string characters one by one, if some mismatches are there, it returns the non-zero values. Let us see the code to get better idea.
Example
#include<iostream> using namespace std; void compareStrings(string s1, string s2) { int compare = s1.compare(s2); if (compare != 0) cout << s1 << " is not equal to "<< s2 << endl; else if(compare == 0) cout << "Strings are equal"; if (compare > 0) cout << s1 << " is greater than "<< s2 << " difference is: " << compare << endl; else if(compare < 0) cout << s2 << " is greater than "<< s1 << " difference is: " << compare << endl; } int main() { string s1("hello"); string s2("helLo"); compareStrings(s1, s2); }
Output
hello is not equal to helLo hello is greater than helLo difference is: 1
- Related Questions & Answers
- Comparing two strings in MySQL?
- Comparing two Strings lexicographically in Java
- Comparing Strings and Portions of Strings in Java
- Comparing two dates in PHP
- Comparing ascii scores of strings - JavaScript
- Comparing Strings with (possible) null values in java?
- Compare two strings lexicographically in C#
- Comparing integers by taking two numbers in JavaScript
- Comparing corresponding values of two arrays in JavaScript
- C++ Program to Concatenate Two Strings
- C program to swap two strings
- How to concatenate two strings in C#?
- Delete Operations for Two Strings in C++
- Largest Merge of Two Strings in C++
- Comparing dates using C#
Advertisements