Home > Backend Development > C#.Net Tutorial > How to use the string comparison function strcmp in C++?

How to use the string comparison function strcmp in C++?

烟雨青岚
Release: 2020-06-23 11:04:27
Original
6181 people have browsed it

How to use the string comparison function strcmp in C++?

#What is the usage of string comparison function strcmp in C?

Function prototype:

int strcmp(const char *s1, const char *s2);1

Header file:

#include 1

Function: Used to compare two strings

Parameters: s1 and s2 are two for comparison. String

Return value: If the strings s1 and s2 are equal, zero is returned; if s1 is greater than s2, a number greater than zero is returned; otherwise, a number less than zero is returned.

Explanation: The strcmp() function compares two strings based on the value of the ACSII code; the strcmp() function first subtracts the first character value of s2 from the first character value of the s1 string. character, if the difference is zero, continue the comparison; if the difference is not zero, return the difference.
Until different characters appear or '\0' is encountered.

Special note: strcmp(const char *s1, const char * s2)Only strings can be compared here, numbers and other parameters cannot be compared.

Code example:

#include <string.h>
int main(void){
char *p="aBc";
char *q="Abc";
char *h="abc";
printf("strcmp(p,q):%d\n",strcmp(p,q));
printf("strcmp(p,h):%d\n",strcmp(p,h));
return 0;}
//结果:
//strcmp(p,q):32
//strcmp(p,h):-32
Copy after login

Recommended tutorial: "C Language"

The above is the detailed content of How to use the string comparison function strcmp in C++?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template