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


C++ strcoll()用法及代碼示例


strcoll()是內置庫函數,並在頭文件中聲明。此函數將str1指向的字符串與str2指向的字符串進行比較。strcoll()函數根據當前語言環境的LC_COLLATE類別的規則執行比較。句法:

int strcoll(const char *str1, const char *str2)

參數:函數strcoll()將兩個字符串作為參數,並返回一個整數值。

Value                   Meaning
less than zero          str1 is less than str2
zero                    str1 is equal to str2
greater than zero       str1 is greater than str2
  1. 小於零:當str1小於str2時
    // C program to illustrate strcoll() 
    #include <stdio.h> 
    #include <string.h> 
      
    int main() 
    { 
        char str1[10]; 
        char str2[10]; 
        int ret; 
      
        strcpy(str1, "geeksforgeeks"); 
        strcpy(str2, "GEEKSFORGEEKS"); 
      
        ret = strcoll(str1, str2); 
      
        if (ret > 0) { 
            printf("str1 is greater than str2"); 
        } else if (ret < 0) { 
            printf("str1 is lesser than str2"); 
        } else { 
            printf("str1 is equal to str2"); 
        } 
      
        return (0); 
    }

    輸出:


    str1 is greater than str2
    
  2. 大於零:當str1大於str2時
    // C program to illustrate strcoll() 
    #include <stdio.h> 
    #include <string.h> 
      
    int main() 
    { 
        char str1[10]; 
        char str2[10]; 
        int ret; 
      
        strcpy(str1, "GEEKSFORGEEKS"); 
        strcpy(str2, "geeksforgeeks"); 
      
        ret = strcoll(str1, str2); 
      
        if (ret > 0) { 
            printf("str1 is greater than str2"); 
        } else if (ret < 0) { 
            printf("str1 is lesser than str2"); 
        } else { 
            printf("str1 is equal to str2"); 
        } 
      
        return (0); 
    }

    輸出:

    str1 is lesser than str2
    
  3. 等於零:當str1等於str2時
    // C program to illustrate strcoll() 
      
    #include <stdio.h> 
    #include <string.h> 
      
    int main() 
    { 
        char str1[10]; 
        char str2[10]; 
        int ret; 
      
        strcpy(str1, "GEEKSFORGEEKS"); 
        strcpy(str2, "GEEKSFORGEEKS"); 
      
        ret = strcoll(str1, str2); 
      
        if (ret > 0) { 
            printf("str1 is greater than str2"); 
        } else if (ret < 0) { 
            printf("str1 is lesser than str2"); 
        } else { 
            printf("str1 is equal to str2"); 
        } 
      
        return (0); 
    }

    輸出:

    str1 is equal to str2
    

相關函數:strcmp(),memcmp()



相關用法


注:本文由純淨天空篩選整理自 strcoll() in C/C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。