📜  C++中的std :: strncmp()

📅  最后修改于: 2021-05-30 20:04:45             🧑  作者: Mango

std :: strncmp()函数从字符比较两个以null结尾的字符串的字符不超过计数,并根据结果返回一个整数。

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

句法 :

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

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

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

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

strcmp()返回什么?

strncmp()函数根据比较结果返回三种不同类型的整数值

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

// C, C++ program to demonstrate
// functionality of strncmp()
  
#include 
#include 
  
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):返回一个负值,如果str1STR2的字符不前num个字符相匹配并且str1字符的ASCII值大于STR2字符的ASCII值较小。结果,我们可以说str2在字典上大于str1

// C, C++ program to demonstrate
// functionality of strncmp()
  
#include 
#include 
  
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):该函数返回零如果与STR2高达num个字符的字符STR1匹配的字符。结果,我们不能说str1等于str2 ,直到num等于任一字符串的长度。

// C, C++ program to demonstrate
// functionality of strncmp()
  
#include 
#include 
  
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值之间的差。

更多例子

范例1:

// CPP program to illustrate strncmp()
#include 
#include 
  
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 
#include 
  
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
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”