您的位置:首页 > 编程语言 > C语言/C++

c语言字符处理函数常见使用集合

2016-09-09 01:04 483 查看
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. 欢迎指正错误
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: