当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++ std::strncmp()用法及代码示例


从字典上比较std::strncmp(函数,对不多于两个以空值结尾的字符串的字符进行比较,并根据结果返回一个整数。

  • 此函数将两个字符串和一个数字num作为参数,并比较两个字符串的最大前num个字节。
  • 最多应等于最长字符串的长度。如果将num定义为大于字符串长度,则比较将一直进行到两个字符串的null-character('\ 0')。
  • 此函数按字典顺序比较两个字符串。它从每个字符串的第一个字符开始比较。如果它们彼此相等,则继续并比较每个字符串的下一个字符,依此类推。
  • 比较过程将一直终止,直到到达一个终止字符串null-character或两个字符串的num个字符都匹配为止。

用法:

int strncmp(const char *str1, const char *str2, size_t count);

参数:
str1 and str2: C string to be compared.
count:Maximum number of characters to compare.
 size_t is an unsigned integral type.

返回值:
Value                               Meaning
Less than zero                      str1 is less than str2.
Zero                                str1 is equal to str2.
Greater than zero                   str1 is greater than str2.

如果任一字符串中的字符数少于计数,则在遇到第一个null时比较结束。


What does strcmp() return?

strncmp()函数在比较的基础上返回三种不同类型的整数值:

1.大于零(> 0):如果在num字符之前str1和str2的字符不匹配并且str1字符的ASCII值大于str2字符的ASCII值,则返回正值。结果,我们可以说str1在字典上大于str2。

// C, C++ program to demonstrate 
// functionality of strncmp() 
  
#include <stdio.h> 
#include <string.h> 
  
int main() 
{ 
    // Take any two strings 
    char str1[10] = "aksh"; 
    char str2[10] = "akash"; 
  
    // Compare strings using strncmp() 
    int result = strncmp(str1, str2, 4); 
  
    if (result == 0) { 
        // num is the 3rd parameter of strncmp() function 
        printf("str1 is equal to str2 upto num characters\n"); 
    } 
    else if (result > 0) 
        printf("str1 is greater than str2\n"); 
    else
        printf("str2 is greater than str1\n"); 
  
    printf("Value returned by strncmp() is:%d", result); 
  
    return 0; 
}

输出:

str1 is greater than str2
Value returned by strncmp() is:18

2.小​​于零(<0):如果str1和str2的字符在num字符之前不匹配并且str1字符的ASCII值小于str2字符的ASCII值,则返回负值。结果,我们可以说str2在字典上大于str1。

// C, C++ program to demonstrate 
// functionality of strncmp() 
  
#include <stdio.h> 
#include <string.h> 
  
int main() 
{ 
    // Take any two strings 
    char str1[10] = "akash"; 
    char str2[10] = "aksh"; 
  
    // Compare strings using strncmp() 
    int result = strncmp(str1, str2, 4); 
  
    if (result == 0) { 
        // num is the 3rd parameter of strncmp() function 
        printf("str1 is equal to str2 upto num characters\n"); 
    } 
    else if (result > 0) 
        printf("str1 is greater than str2\n"); 
    else
        printf("str2 is greater than str1\n"); 
  
    printf("Value returned by strncmp() is:%d", result); 
  
    return 0; 
}

输出:

str2 is greater than str1
Value returned by strncmp() is:-18

3.等于零(0):如果str1的字符与str2的字符(最多num个字符)匹配,则此函数返回零。结果,我们不能说str1等于str2,直到num等于任一字符串的长度。

// C, C++ program to demonstrate 
// functionality of strncmp() 
  
#include <stdio.h> 
#include <string.h> 
  
int main() 
{ 
    // Take any two strings 
    char str1[10] = "akash"; 
    char str2[10] = "akas"; 
  
    // Compare strings using strncmp() 
    int result = strncmp(str1, str2, 4); 
  
    if (result == 0) { 
        // num is the 3rd parameter of strncmp() function 
        printf("str1 is equal to str2 upto num characters\n"); 
    } 
    else if (result > 0) 
        printf("str1 is greater than str2\n"); 
    else
        printf("str2 is greater than str1\n"); 
  
    printf("Value returned by strncmp() is:%d", result); 
  
    return 0; 
}

输出:

str1 is equal to str2 upto num characters
Value returned by strncmp() is:0

注意:当字符串不同时,在两种情况下,您都会发现strncmp()函数返回的值是str1和str2中第一个不匹配字符的ASCII值之间的差。

More Examples


范例1:

// CPP program to illustrate strncmp() 
#include <cstring> 
#include <iostream> 
  
void display(char* abc, char* xyz, int res, int count) 
{ 
    if (res > 0) 
        std::cout << xyz << " come-before " << abc; 
    else if (res < 0) 
        std::cout << abc << " come-before " << xyz; 
    else
        std::cout << "First " << count << " characters of string " 
        << abc << " and " << xyz << " are same"; 
} 
  
int main() 
{ 
    char abc[] = "GeeksforGeeks"; 
    char xyz[] = "Geeks"; 
    int res; 
    res = std::strncmp(abc, xyz, 4); 
    display(abc, xyz, res, 4); 
    return 0; 
}

输出:

First 4 characters of string GeeksforGeeks and Geeks are same

范例2:

// CPP program to illustrate strncmp() 
#include <cstring> 
#include <iostream> 
  
void display(char* abc, char* xyz, int res, int count) 
{ 
    if (res > 0) 
        std::cout << xyz << " come-before " << abc; 
    else if (res < 0) 
        std::cout << abc << " come-before " << xyz; 
    else
        std::cout << "First " << count << " characters of string " <<  
        abc << " and " << xyz << " are same"; 
    ; 
} 
  
int main() 
{ 
    char abc[] = "GeeksforGeeks"; 
    char xyz[] = "Geeks"; 
    int res; 
    res = std::strncmp(abc, xyz, 6); 
    display(abc, xyz, res, 6); 
    return 0; 
}

输出:

Geeks come-before GeeksforGeeks


相关用法


注:本文由纯净天空筛选整理自 std::strncmp() in C++。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。