Author name: Shubham Das

Differences between C++ string == and compare()

In C++, the ‘==’ operator and the `compare()` member function of the `std::string` class are both used to compare strings, but they have different characteristics and behaviors. Here are the main differences between the two: 1. Syntax Using ‘==’ Operator std::string str1 = “hello”; std::string str2 = “world”; if (str1 == str2) { // strings …

Differences between C++ string == and compare() Read More »

std::string::compare() in C++ with Examples

The std::string::compare() function in C++ is used to compare two strings lexicographically. It is a member function of the std::string class defined in the <string> header. The function returns an integer value that indicates the relationship between the two strings being compared. Syntax: int compare(const string& str) const; int compare(const char* s) const; int compare(size_t …

std::string::compare() in C++ with Examples Read More »

Write a C++ program to find the last element of an array

Here, we discuss how we find the last element of an array using C++. CODE #include<iostream> using namespace std; int main(){ int arr[]={5,6,7,8,9} int size = sizeof(arr) / sizeof(arr[0]); int lastElement = arr[size – 1]; cout<<“The last element of the array is: “<<lastElement<<endl; return 0; } OUTPUT The last element of the array is: 9 …

Write a C++ program to find the last element of an array Read More »

Scroll to Top