Table of Contents
Compare String In C++
In the realm of C++, string comparison forms a fundamental aspect of programming. However, the journey to compare strings isn’t a one-size-fits-all endeavor. There exist diverse methods, each with its own purpose and intricacies.
Whether you’re a novice or a seasoned programmer, understanding these various techniques empowers you to tackle different scenarios effectively. This guide delves into the multiple ways of comparing strings in C++, offering insights into their nuances and helping you choose the optimal approach for your coding needs.
Ways to Compare String in C++
There are different ways to compare the strings in the C++ programming language, as follows:
- Using strcmp() function
- Using compare() function
- Using Relational Operator
- Using For loop and If statement
- Using user-defined function
1.Strcmp() Function
The strcmp() function is a ready-to-use function found in the string.h header file. It serves to compare two strings in a lexicographical manner. This involves systematically comparing characters between the two strings, starting from the first character and progressing until all characters match or a NULL character is encountered in either string.
- int strcmp ( const char *leftstr, const char *rightstr );
Parameters:
leftstr: It defines the characters of the left string.
rightstr: It defines the characters of the right string.
Returns:
The leftstr string compares each character with the second string from the left side till the end of both strings. And, if both the strings are equal, the strcmp() function returns strings are equal. Else, the strings are not equal.
2. Compare() Function
The compare() function is a built-in function in C++. It’s used to compare two strings and gives specific results depending on the comparison:
- If the strings are exactly the same, the function returns 0.
- If the character value of the first string is lower than the second, the function returns a value less than 0.
- If the second string is greater than the first, the function returns a value greater than 0.
Syntax
int compare (const string &str) const;
3. Relational Operator
In C++, the relational operator is employed to compare both strings and numerical values. C++ features various relational operators, including ‘==’, ‘!=’, >, and <. In this context, we focus on two operators: ‘==’ for equality and ‘!=’ for inequality. These operators make string comparisons straightforward and efficient.
Syntax
- String1 == string2 // here, we use double equal to the operator
- Or
- String1 != string2 // here, we use not equal to operator
4. Using For loop and If statement
In C++, you can compare strings using a for
loop and a if
statement. Here’s a simple example demonstrating this approach:
#include <iostream>
#include <string>
int main() {
std::string str1 = "Hello";
std::string str2 = "World";
bool equal = true;
if (str1.length() != str2.length()) {
equal = false;
} else {
for (size_t i = 0; i < str1.length(); ++i) {
if (str1[i] != str2[i]) {
equal = false;
break;
}
}
}
if (equal) {
std::cout << "Strings are equal." << std::endl;
} else {
std::cout << "Strings are not equal." << std::endl;
}
return 0;
}
In this example, the for
loop iterates through each character of both strings and checks if they are equal at each position. If a character mismatch is found, the equal
flag is set to false , and the loop is exited early. Finally, based on the value of the equal
flag, the program prints whether the strings are equal or not.
5. Using user-defined function
You can create a user-defined function to compare strings in C++. Here’s an example:
#include <iostream>
#include <string>
bool areStringsEqual(const std::string& str1, const std::string& str2) {
if (str1.length() != str2.length()) {
return false;
}
for (size_t i = 0; i < str1.length(); ++i) {
if (str1[i] != str2[i]) {
return false;
}
}
return true;
}
int main() {
std::string str1 = "Apple";
std::string str2 = "Apple";
if (areStringsEqual(str1, str2)) {
std::cout << "Strings are equal." << std::endl;
} else {
std::cout << "Strings are not equal." << std::endl;
}
return 0;
}
In this example, the areStringsEqual
function takes two string arguments and performs the same comparison as before. The main
function calls this user-defined function, making the code more organized and reusable.
FAQ – Different Way To Compare String In C++
Q1.How to compare two C strings in C++?
Ans. strcmp() is a C library function that compares two strings lexicographically. It can also inbuilt function in C++ String. Syntax: strcmp() function will take only two strings (character arrays) as arguments and returns: 0, if both the strings are equal i.e. firstString is lexicographically equal to secondString.
Q2. What is the string comparison function in C?
Ans. The strcmp() function, functions to compare the strings (str1,str2). The strings str1 and str2 will be compared using this function. If the function returns a value of 0, it signifies that the strings are equal otherwise, the strings are not equal.
Q3. What does == do in strings C++?
Ans. In C++ the == operator is overloaded for the string to check whether both strings are the same or not. If they are the same it will show 1, otherwise 0. So it is like a Boolean-type function
Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together