當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。