GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — C++ cstring strcmp() Function
Selected Reading
C++ cstring strcmp() FunctionThe C++ cstring strcmp() Function compares two strings and returns the difference between them. This function is similar to the memcmp() function, but the memcmp() function returns the difference between memory buffers, whereas, this function returns the difference between the two strings. This function is useful when we don't have to write complex conditional statements to compare two strings. This function is case-sensitive, means "Hello" and "hello" are not equal. SyntaxFollowing is the syntax of the C++ cstring strcmp() function − int strcmp(const char *str1, const char *str2); ParametersFollowing are the parameters of this function −
Return ValuesThis function returns −
Example 1Following is an example to show the usage of strcmp() function. In this, we will compare two strings str1 and str2 using strcmp() function. #include <iostream>
#include <cstring>
using namespace std;
int main() {
char str1[15];
char str2[15];
int ret;
strcpy(str1, "abcdef");
strcpy(str2, "ABCDEF");
ret = strcmp(str1, str2);
if(ret > 0) {
cout << "str1 is greater than str2"<<endl;
} else if(ret < 0) {
cout << "str1 is less than str2"<<endl;
} else {
cout << "str1 is equal to str2"<<endl;
}
return 0;
}
OutputFollowing is the output of the above C++ program: str1 is less than str2 Example 2Now, Let's take two strings str1 and str2, both will be the same but one will be in uppercase and another in lowercase. We then compare these two strings using strcmp() function. #include <iostream>
#include <cstring>
using namespace std;
int main() {
char str1[15];
char str2[15];
int ret;
strcpy(str1, "Hello");
strcpy(str2, "hello");
ret = strcmp(str1, str2);
if(ret > 0) {
cout << "str1 is greater than str2"<<endl;
} else if(ret < 0) {
cout << "str1 is less than str2"<<endl;
} else {
cout << "str1 is equal to str2"<<endl;
}
return 0;
}
OutputFollowing is the output of the above C++ program − str1 is less than str2 Advertisements
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |