C语言字符串操作函数
来源:互联网 发布:linux空间不足 编辑:程序博客网 时间:2024/05/05 02:13
1.strlen()
函数原型:size_t strlen(char const *string),返回返回字符串string长度,返回值类型size_t是头文件stddef.h中定义的,表示无符号整型,也因为这一返回值类型,使用strlen()时需注意.如:if( strlen( x ) >= strlen( y ) )...和if( strlen( x ) - strlen( y ) >= 0 )...第一种方式能够像预想的那样工作,第二种if语句永远为真,因为strlen()的结果是无符号数,所以操作符>=左边的表达式是无符号数,而无符号数绝不会小于0(这是C和指针里提示的,但是用dev c++和vs测试这几种方式都是能正常工作的,不过还是尽量避免错误的写法好).
2.strcpy
函数原型:char *strpy( char *dst, char const *src ),将src字符串复制到dst参数中,如果两参数内存中重叠了,结果是未定义的.如果dst不够长,strcpy将会继续访问dst之后的内存,修改那里的变量,为了安全起见,要保证dst足够长.同时注意如下声明;char *p = "hello"; strcpy( p, "12" );这是不允许的,因为p指向字符串常量,无法修改.
3.strcat
函数原型:char *strcat( char *dst, char const *src ),将src的拷贝添加到dst末尾.如果dst和src位置重叠,结果是未定义的.
4.strcmp
函数原型:int strcmp( char const *s1, char const *s2 ),如果s1小于s2,返回一个小于0的数(注意不是-1),反之返回大于0的数,相等返回0.
5.strncpy strncat strncmp
函数原型:char *strncpy( char *dst, char const *src, size_t len ),char *strncat( char *dst, char const *src, size_t len ),int strncpy( char const *s1, char const *s2, size_t len ),
注意strncpy中,如果strlen(src)小于len,dst将被额外的NUL字节填充,如果strlen(src)大于len,只会 复制len个字符,这也导致了dst结果可能不是以NUL字节结尾了!(刚好dst中的NUL被复制成src中的字符时,这也是C和指针中提示的,不过用dev c++测试并不会这样).
6.strchr
函数原型:char *strchr( char const *str, int ch ),注意第二个参数是整数,函数功能是在str中查找ch第一次出现的位置找到后返回一个指向该位置的指针,没找到则返回NULL.strrchr功能和strchr相近,返回的是ch出现的最右边的位置.
7.strpbrk
函数原型:char *strpbrk( char const *str, char const *group ),返回一个指向str中第一个匹配group中任意一个字符的字符位置,没有则返回NULL.
8.strstr
函数原型:char *strstr( char const *s1, char const *s2 ),返回s2整个出现在s1中的位置.
9.strspn和strcspn
函数原型:size_t strspn( char const *str, char const *group ),size_t strspn( char const *str, char const *group )计算str起始位置中匹配group中任意字符的字符数,ptr = buffer + strspn( buffer, " \n\r\f\t\v" );--有个空格.ptr将会指向buffer中第一个非空白字符.如buffer是“ hello”,ptr将会指向h省略前面的空格.好像说的不清楚,C和指针里的描述一开始也误导我了,还是看代码能明白啊.百度百科源码如下:
int strspn(const char *s,const char *accept){ const char *p; const char *a; int count = 0; for(p = s; *p != '\0'; ++p) { for (a = accept; *a != '\0'; ++a) { if (*p == *a) { ++count; break; } }//里面的for循环到此为止 if (*a == '\0') { return count; } //++count; }//外面的for循环到此为止 return count;}
char buffer[] = "25,142,330,Smith,J,239-4123";
len1 = strspn( buffer, "0123456789" );
len2 = strspn( buffer, ",0123456789" );len1 = 2,len2 = 11;因为第二个语句group多了个逗号.
总的来说就是str起始位置开始,第一个字符出现在group中则count+1,然后第二个也在group中则再加1,直到没找到为止.
strcspn与strspn相反.
10.strtok
函数原型:char *strtok( char *str, char sonst *sep ),将str用sep中的字符分隔开(也就是将str中的分隔标记用'\0'取代).
#include <stdio.h>#include <stdlib.h>#include <string.h>char * __cdecl strtok ( char * string, const char * control ){ char *str; const unsigned char *ctrl = control; unsigned char map[32]; int count; static char *nextoken; //保存剩余子串的静态变量 /* Clear control map */ for (count = 0; count < 32; count++) map[count] = 0; /* Set bits in delimiter table */ do { map[*ctrl >> 3] |= (1 << (*ctrl & 7));//将256个ASCII字符保存到map[32]中,共4个字节的存储空间,相当于每个//字符占一个二进制位,map[*ctrl >> 3]是先找到*ctrl将映射到哪一位,如'a'为97,除以8(右移3位)为12,则'a'将映射在map[12]中,之//后'a'后三位和7与得到'a'除以8的余数,如'c'为99,97&7 = 3,之后将1移右移相应3位数后与map相或即可映射到map中. } while (*ctrl++); /* Initialize str. If string is NULL, set str to the saved * pointer (i.e., continue breaking tokens out of the string * from the last strtok call) */ if (string) str = string; //第一次调用函数所用到的原串 else str = (char *)nextoken; //将函数第一参数设置为NULL时调用的余串 /* Find beginning of token (skip over leading delimiters). Note that * there is no token iff this loop sets str to point to the terminal * null (*str == '\0') */ while ( (map[*str >> 3] & (1 << (*str & 7))) && *str )//如果str开头就是截取标记,则可以直接跳过 str++; string = str; //此时的string返回余串的执行结果 /* Find the end of the token. If it is not the end of the string, * put a null there. *///这里就是处理的核心了, 找到分隔符,并将其设置为'\0',当然'\0'也将保存在返回的串中 for ( ; *str ; str++ ) if ( map[*str >> 3] & (1 << (*str & 7)) ) { *str++ = '\0'; //这里就相当于修改了串的内容 ① break; } /* Update nextoken (or the corresponding field in the per-thread data * structure */ nextoken = str; //将余串保存在静态变量中,以便下次调用 /* Determine if a token has been found. */ if ( string == str ) return NULL; else return string;}int main(){ char input[]="abcd,efg,hijk,lmn"; char *p; p = strtok(input, ",");/*注意这里不能写成p = strtok("abcd,efg,hijk,lmn", ",");因为"abcd,efg,hijk,lmn"是字符串常量,strtok将会对其进行修改,这是不允许的.*/ while(p) {printf( "%s\n", p );p = strtok(NULL, ",");} return 0;}
这里的strtok源码是google的,我删除了其中一些不必要的代码.通过这个理解strtok更直接,观察源码可知有个静态变量
static char *nextoken.因为它的存在,才有了main函数中strtok的调用方式,也因为它的存在,strtok不能同时解析两个字符串.
源码地址:http://www.cnblogs.com/hoys/archive/2011/09/19/2180999.html
- C语言字符串操作函数
- C语言字符串操作函数
- C语言字符串操作函数
- C语言字符串操作函数
- C语言字符串操作函数
- C语言字符串操作函数
- C语言字符串操作函数
- C语言字符串操作函数
- C语言 字符串操作函数
- C语言字符串操作函数
- c语言字符串操作函数
- C语言字符串操作函数
- C语言字符串操作函数
- c语言字符串操作函数
- C语言字符串操作函数
- C语言字符串操作函数
- c语言字符串操作函数
- C语言字符串操作函数
- hdu 1098 数学归纳法
- 通过自定义协议在BS里面启动CS程序
- Objective-C中的继承与多态, Category, Extension
- dancing links解决X问题的C++实现
- JAVA反射机制
- C语言字符串操作函数
- Android:dimen尺寸资源文件的使用
- 《leetCode》:Sqrt(x)----------hard
- 达内学习日志项目四:云笔记项目(书写规范)
- Android:View中的performClick()触发条件
- ascyhttpclient 使用心得
- linux c进程间通信
- android 解决listview 内部有按键控件时,listview无法点击
- Adaboost 算法的原理与推导