c語言字符處理函數常見使用集合


1.最近看一些開源項目代碼時,總會看到 c 語言中一些  "str" 開頭的處理字符串的用法,有的之前沒用到過,特此記錄,隨時看到隨時添加。

  這里不提出源碼,只是一些使用說明加例子:

1).unsigned long int strtoul(const char *nptr, char **endptr, int base);(類似還有atoi,atof, strtoi, strtol等)

描述:  

  strtoul()會將參數nptr字符串根據參數base來轉換成無符號的長整型數。參數base范圍從2至36,或0。

  參數base代表采用的進制方式,如base值為10則采用10進制,若base值為16則采用16進制數等。

  當base值為0時會根據情況選擇用哪種進制:如果第一個字符是'0',就判斷第二字符如果是‘x’則用16進制,否則用8進制;第一個字符不是‘0’,則用10進制。

  一開始strtoul()會掃描參數nptr字符串,跳過前面的空格字符串,直到遇上數字或正負符號才開始做轉換,再遇到非數字或字符串結束時('')結束轉換,並將結果返回。若參數endptr不為NULL,則會將遇到不合條件而終止的nptr中的字符指針由endptr返回。

理解:

  個人覺得使用這個函數的時候,首先要清楚nptr所指的內容是什么進制表達的數,例如"0xff"首先這是個16進制的數,然后你是想要知道這個16進制的數的無符號長整形對應的值是多少。

  類似的還有“1234”, “-1234” 等將其轉化為對應的無符號數。用錯的方式有多種,但是正確的就一種,就是nprt指向的內容可以得到你想要的合法的base進制表達的值.

測試程序:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>

#define BUFSIZE 1024

int
main(int argc, char *argv[])
{    
    char s1[BUFSIZE];
    int base;
    while(scanf("%s %d", s1, &base) == 2)
    {
        printf("result : %u\n", strtoul(s1, NULL, base));
        if (errno)
        {
            printf("errno %d\n", errno);
        }
    }

    return 0;
}    

操作:輸入nptr的內容 和 base值, result:即為轉換結果。

八進制的轉換規則:

十進制到八進制規則:和十進制轉二進制一樣,除8取每次的余數,然后余數前后轉置

八進制轉十進制:和二進制轉十進制一樣,對應位乘上 8^x次方,然后相加,例:72(8)-> 2*8^0 + 7*8^1 = 16 + 56 = 72;

 

有符號整數轉無符號整數:整數相等;負數,符號位在內取反再加1. 

2. char * strchr(const char *s, int c), char * strrchr(const char *s, int c);

描述:strchr找到字符c在字符串s中第一次出現的位置,若找到返回該位置的地址,如沒找到返回NULL。

   strrchr 找到字符c在字符串s中最后一次出現的位置,若找到返回該位置的地址,如沒找到返回NULL。

 

3.size_t strspn(const char *s, const char *accept), size_t strcspn(const char *s, const char *reject)

描述: strspn, s中從第一個字符開始判斷是該字符是否屬於accept,若屬於則繼續往下判斷,若不屬於返回該字符的在s中的下標,依次類推。s中字符下標從0開始

例子程序:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>

#define BUFSIZE 1024

int
main(int argc, char *argv[])
{    
    char s1[BUFSIZE];
    char s2[BUFSIZE];
    int base;
    while(scanf("%s %s", s1, s2) == 2)
    {
        printf("result : %d\n", strspn(s1, s2));
    }

    return 0;
}    

測試結果:

下面是lsocket中用到該函數的地方:

/* _needsnolookup
 * 
 * helper function: checks if the address consists only of chars that
 * make up a valid ip(v4 or v6) address, and thus needs no nslookup.
 * 
 * Arguments:
 *     addr    address to check
 * 
 * Returns:
 *     1 if the address consists only of chars that make up a valid ip(v4
 *     or v6) address, 0 otherwise.
 * 
 * Note: this does not check whether the address is a valid ip address,
 * just whether it consists of chars that make up one.
 */
static int _needsnolookup(const char *addr)
{
    int len = strlen(addr);
    int pfx = strspn(addr, "0123456789.");
    if (pfx != len) {
        pfx = strspn(addr, "0123456789abcdefABCDEF:");
        /* last 2 words may be in dot notation */
        if (addr[pfx] == '.') {
            int lpfx = strrchr(addr, ':') - addr;
            if (lpfx <= 0 || lpfx > pfx) return 0;
            pfx = lpfx + 1 + strspn(addr + lpfx + 1, "0123456789.");
        }
    }
    return pfx == len;
}

 

4. int strcasecmp(const char *s1, const char *s2); int strncasecmp(const char *s1, const char *s2);

描述:比較兩個字符串的大小,相同返回i,不同若在第i個字符出不等則返回s1[i] - s2[2]的值,最中要的一點是,s1, s2部分大小寫。

理解:和strcmp , strncmp實際上用發相同,只不過是比較的兩個字符串部分大小寫即 a = a, a = A ...

代碼:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>

#define BUFSIZE 1024

int
main(int argc, char *argv[])
{    
    char s1[BUFSIZE];
    char s2[BUFSIZE];
    int base;
    while(scanf("%s %s", s1, s2) == 2)
    {
        printf("result : %d\n", strcasecmp(s1, s2));
    }

    return 0;
}   

測試結果:

 

待添加。

Hope this is helpful for you. 歡迎指正錯誤

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM