c语言 strcmp函数

Hello, folks! In this article, we will be focusing on the working of the C++ strcmp function in detail.

大家好! 在本文中,我们将重点关注C ++ strcmp函数的详细工作。

C ++ strcmp()函数入门 (Getting started with C++ strcmp() function)

C++ strcmp() function is an efficient way to compare two strings lexiographically.

C ++的strcmp()函数是比较两个串的有效方式lexiographically

The strcmp() function compares two input strings in a lexicographic manner and returns an integer value based on the outcome of the comparison of the two input strings.

strcmp()函数以字典方式比较两个输入字符串,并根据两个输入字符串的比较结果返回一个整数值。

By lexicographic comparison, we mean, that two strings are actually compared with their ASCII values of every character. If found same, zero is returned and the string is said to be equal.

通过词典比较,我们的意思是, 实际上将两个字符串与每个字符的ASCII值进行比较 。 如果发现相同,则返回零,并说该字符串相等。

Syntax:

句法:


strcmp(string1,string2)

Example:

例:


#include<stdio.h>
#include<string.h> int main()
{  char str1[] = "Python"; char str2[] = "Python"; if(strcmp(str1, str2)==0)printf("Strings are equal");elseprintf("\nThe strings are not equal");return 0;
} 

Output:

输出:


Strings are equal


strcmp()函数返回的值 (Value returned by the strcmp() function)

The strcmp() compares the strings lexicographically and returns either of the following values based on the comparison:

strcmp()按字典顺序比较字符串,并根据比较结果返回以下两个值之一:

  • value = zero(0): The function returns 0, if both the strings are lexicographically equal i.e. both the strings are identical.value = zero(0) :如果两个字符串在字典上相等,即两个字符串相同,则该函数返回0。
  • value > zero(0): It returns a value greater than zero, if the first non-matching character of the string1 has a greater ASCII value than the character present in the string2.value > zero(0) :如果string1的第一个不匹配字符的ASCII值大于string2中出现的字符,则返回大于零的值。
  • value < zero(0): It returns a value less than zero, if the first non-matching character of the string1 has less ASCII value than the character present in the string2.value < zero(0) :如果string1的第一个不匹配字符的ASCII值小于string2中出现的字符,则返回小于零的值。


变体1:strcmp()函数返回零(0) (Variant 1: The strcmp() function returns zero(0))

The strcmp() function returns zero if both the strings are identical.

如果两个字符串相同,则strcmp()函数将返回

Example:

例:


#include<stdio.h>
#include<string.h> int main()
{  char str1[] = "Python"; char str2[] = "Python"; int val_strcmp=0;val_strcmp = strcmp(str1, str2);printf("The return value of strcmp() function: %d" , val_strcmp); return 0;
} 

Output:

输出:


The return value of strcmp() function: 0


形式2:strcmp()函数返回的值“大于零” (Variant 2: The strcmp() function returns a value ‘greater than zero’)

A value greater than zero is returned if the ASCII value of the first non-matching character of string1 is found to be greater than the ASCII value of the corresponding character of string2.

如果发现string1的第一个不匹配字符的ASCII值大于string2的相应字符的ASCII值,则返回大于零的值。

Lets’s take an example:

让我们举个例子:

string1 = ‘yz’

string1 ='yz'

string2 = ‘ab’

string2 ='ab'


strcmp(string1,string2)

When two strings are not identical, the function returns the difference between the ASCII values of the first non-matching characters of the two strings.

当两个字符串不相同时,该函数将返回两个字符串的第一个不匹配字符的ASCII值之间的差。

In the above example, the function returns 24 because ASCII value of ‘y’ – ASCII value of ‘a’ is 24

在上面的示例中,该函数返回24,因为ASCII值“ y”-ASCII值“ a”为24

i.e 121 – 97 = 24

即121 – 97 = 24

Example:

例:


#include<stdio.h>
#include<string.h> int main()
{  char str1[] = "wxyz"; char str2[] = "abcd"; int val_strcmp=0;val_strcmp = strcmp(str1, str2);printf("The return value of strcmp() function: %d" , val_strcmp); return 0;
} 

In the above snippet of code, the ASCII value of ‘w’ – ASCII value of ‘a’ is more than zero i.e. 119 – 97 = 22

在上面的代码片段中,“ w”的ASCII值–“ a”的ASCII值大于零,即119 – 97 = 22

Output:

输出:


The return value of strcmp() function: 22


变体3:strcmp()函数返回的值小于零 (Variant 3: The strcmp() function returns a value ‘less than zero’)

The strcmp() function returns a value less than zero, if the ASCII value of the first non-matching character of the first string is less than the ASCII value of that particular character of the second string.

如果第一个字符串的第一个不匹配字符的ASCII值小于第二个字符串的那个特定字符的ASCII值,则strcmp()函数返回小于零的值。

Consider the strings, string1 = ‘ab’ and string2 =’yz’

考虑字符串string1 ='ab'和string2 ='yz'

The ASCII value of ‘a’ is less than ASCII value of ‘y’ i.e. 97 – 121 = -24.

ASCII值“ a”小于ASCII值“ y”,即97 – 121 = -24。

Thus, the value returned by the function is less than zero i.e. -24.

因此,函数返回的值小于零,即-24。

Example:

例:


#include<stdio.h>
#include<string.h> int main()
{  char str1[] = "ab"; char str2[] = "yz"; int val_strcmp=0;val_strcmp = strcmp(str1, str2);printf("The return value of strcmp() function: %d" , val_strcmp); return 0;
} 

Output:

输出:


The return value of strcmp() function: -24


C ++ strcmp()方法一目了然! (C++ strcmp() method at a glance!)

  • The strcmp() function compares the characters of the strings in a lexicographic manner.strcmp()函数以字典方式比较字符串的字符。
  • Moreover, the function returns an integer based on the comparison.此外,该函数根据比较结果返回一个整数。
  • An integer value greater than zero is returned if the ASCII value of the first non-matching character of the first string is greater than the second one.如果第一个字符串的第一个不匹配字符的ASCII值大于第二个,则返回大于零的整数值。
  • If the strings are identical, the function returns zero.如果字符串相同,则函数返回零。
  • A value less than zero is returned by the function if the ASCII value of the first non-matching character of the first string is less than the second one如果第一个字符串的第一个不匹配字符的ASCII值小于第二个,则函数返回小于零的值


结论 (Conclusion)

Thus, in this article, we have understood the working of strcmp() function in C++.

因此,在本文中,我们了解了C ++中strcmp()函数的工作方式。



参考资料 (References)

  • String comparison — JournalDev字符串比较— JournalDev

翻译自: https://www.journaldev.com/38264/c-plus-plus-strcmp-function

c语言 strcmp函数

c语言 strcmp函数_了解C ++ strcmp()函数相关推荐

  1. mysql独有的函数_数据库之MySQL函数(一)

    一.数学函数 1.绝对值函数 ABS(x) :返回 x 的绝对值 mysql> select ABS(2),ABS(-2.3),ABS(-22); 返回的结果如下: 数学学得好的大佬应该知道(本 ...

  2. subtotal函数_星期五的Excel函数:将总计为SUBTOTAL的筛选列表

    subtotal函数 The Excel SUM function does a great job of adding numbers on a worksheet, and it's probab ...

  3. 编写分段函数子函数_编写自己的函数

    编写分段函数子函数 PYTHON编程 (PYTHON PROGRAMMING) In Python, you can define your own functions. 在Python中,您可以定义 ...

  4. java 高阶函数_谈谈高阶函数给我们带来了什么。

    什么是高阶函数 一句话解释的话,就是函数可以作为参数以变量的方式持有,引用,构造和使用 一些文章我随手搜的,大家可以看看,它本身的概念并不复杂. 高阶函数-廖雪峰 高阶函数-wiki 其实现在主流语言 ...

  5. java调用javascript函数_[Java教程]JavaScript函数的4种调用方法详解

    [Java教程]JavaScript函数的4种调用方法详解 0 2016-08-09 00:00:12 在JavaScript中,函数是一等公民,函数在JavaScript中是一个数据类型,而非像C# ...

  6. js 实现2的n次方计算函数_密码杂凑函数的基本性质探讨

    密码学研究的宗旨是保证数据和通信的机密性.完整性和认证性,其中完整性和认证性的实现依赖于一类关键的密码学函数---密码杂凑函数.密码杂凑函数通常用来计算数据的短"指纹"(也称杂凑值 ...

  7. 什么是python函数_什么是python函数

    python函数是指组织好的.可重复使用的.用来实现单一或相关联功能的代码段.python函数包含系统中自带的一些函数.第三方函数.以及用户自定义的函数. 函数是可以实现一些特定功能的小方法或是小程序 ...

  8. python四大高阶函数_四大高阶函数

    目录 1. 匿名函数 在我们需要一个函数但又不想费神的去命名一个函数的场合下使用,这就是匿名函数 1 f = lambda x,y,z:x+y+z2 defF(x,y,z):3 return x+y+ ...

  9. 画分段函数_秃头节:“函数”段子已出炉高中数学题型分析

    高中数学函数题型整理解析版 函数图像 有关函数图象识别问题的常见题型及解题思路(1)由函数的定义域,判断图象左右的位置,由函数的值域,判断图象的上下位置:②由函数的单调性,判断图象的变化趋势:③由函数 ...

  10. ltrim函数_数据分析常用Excel函数

    Excel是我们工作中经常使用的一种工具,对于数据分析来说,这也是处理数据最基础的工具.本文介绍数据分析中最常用的Excel函数. 查找匹配类 文本数据清洗类 日期类 逻辑运算类 计算统计类 一.查找 ...

最新文章

  1. 使用Eclipse+PyDev+EclipseHtmlEditor搭建Django开发环境
  2. appendChild append insertBefore prepend
  3. 拥抱 Java 8 并行流吧,速度飞起!
  4. 如何解决failed to push some refs to git
  5. 什么是JNDI,SPI,CCI,LDAP和JCA?
  6. VM虚拟机 安装OS X 错误vcpu-0:VERIFY vmcore/vmm/main/physMem_monitor.c:1123
  7. C# Winform 窗体美化(目录)
  8. CodeFirst实体类中,为什么都把ICollectionx定义成virtual?
  9. 华为2017.7.26机试
  10. Word字体修改(罚抄,抄作业专用)
  11. html5 机构化元素
  12. java 传递intent_intent传递参数
  13. java实现图片去除底色,图片变成透明背景
  14. Nginx正则表达式与location匹配简介
  15. 复制链接到剪切板php,剪切复制粘贴
  16. 【数据结构和算法】基础之素数
  17. 使用Word制作文档封面
  18. 文献记录(part23)--Learn to model blurry motion via directional similarity and filtering
  19. 2021软科计算机科学与技术,2021软科世界一流学科即将重磅发布!
  20. Win10安装安卓模拟器入坑记

热门文章

  1. 深度优先搜索广度优先搜索
  2. 经典卷积网络——DenseNet代码实现
  3. gerrit搭建过程
  4. h.264 NALU详细分析2
  5. 美的空调室外机工作异常综合案例分析与检修
  6. Mockito 的 MockMvc:零基础教程
  7. 【软件安装】Visual Studio 2013~2019全系列离线安装包在线安装器
  8. 逻辑斯蒂回归java_逻辑斯蒂回归模型
  9. 回归分析:逻辑斯蒂回归模型,非线性分类任务案例
  10. springboot2整合activiti6过程总结