
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Case-insensitive string comparison in C++
In C++ we have strings in standard library. In this program we will see how to check whether two strings are identical or not. In this case we will ignore the case.
Here the logic is simple. We will convert the whole string into lowercase or uppercase strings, then compare them, and return the result.
We have used the algorithm library to get the transform function to convert the string into lowercase string.
Input: Two strings “Hello WORLD” and “heLLO worLD” Output: Strings are same
Algorithm
Step 1: Take two strings str1, and str2 Step 2: Convert str1, and str2 into lowercase form Step 3: Compare str1 and str2 Step 4: End
Example Code
#include<iostream> #include <algorithm> using namespace std; int case_insensitive_match(string s1, string s2) { //convert s1 and s2 into lower case strings transform(s1.begin(), s1.end(), s1.begin(), ::tolower); transform(s2.begin(), s2.end(), s2.begin(), ::tolower); if(s1.compare(s2) == 0) return 1; //The strings are same return 0; //not matched } main() { string s1, s2; s1 = "Hello WORLD"; s2 = "heLLO worLD"; if(case_insensitive_match(s1, s2)) { cout << "Strings are same"; }else{ cout << "Strings are not same"; } }
Output
Strings are same
- Related Articles
- How do I make my string comparison case insensitive?
- How do we make my string comparison case insensitive in java?
- How do I do a case insensitive string comparison in Python?
- How to do case insensitive string comparison of strings in JavaScript
- Case sensitive string comparison in Java.
- Case-insensitive string replacement using Python Program
- Case insensitive search in Mongo?
- Case-insensitive Dictionary in C#
- How to do case-sensitive string comparison in JavaScript?
- How MySQL can perform case-sensitive string comparison?
- MySQL case-insensitive DISTINCT?
- Is Python case-sensitive or case-insensitive?
- How to make SQL case sensitive string comparison in MySQL?
- How to use case-insensitive switch-case in JavaScript?
- JavaScript Count characters case insensitive

Advertisements