章节重点
求字符串长度:strlen
长度不受限制的字符串函数:strcpy、strcat、strcmp
长度受限制的字符串函数:strncpy、strncat、strncmp
字符串查找:strstr、strtok
错误信息报告:strerror
字符操作
内存操作函数:memcpy、memmove、memset、memcmp
C语言当中对字符和字符串的处理很是频繁,但是C语言本身是没有字符串类型的,字符串通常放在常量字符串中或者字符数组中。字符串常量适用于那些对他不做修改的字符串函数。
函数介绍
strlen
http://www.cplusplus.com/reference/cstring/strlen/?kw=strlen
size_t strlen ( const char * str );
字符串已经 '\0' 作为结束标志,strlen函数返回的是在字符串中 '\0' 前面出现的字符个数(不包含 '\0' )。
参数指向的字符串必须要以 '\0' 结束。
注意函数的返回值为size_t,是无符号的( 易错 )
学会strlen函数的模拟实现
size_t my_strlen(const char *string )
{
if(*string == '\0')
return 0;
return my_strlen(string+1)+1;
}
strcpy
http://www.cplusplus.com/reference/cstring/strcpy/
char* strcpy ( char * destination , const char * source );
Copies the C string pointed by source into the array pointed by destination, including thterminating null character (and stopping at that point).
源字符串必须以 '\0' 结束。
会将源字符串中的 '\0' 拷贝到目标空间。
目标空间必须足够大,以确保能存放源字符串。
目标空间必须可变。
学会模拟实现。
char* my_strcpy(char *strDestination, const char *strSource )
{
//1、判断参数的有效性
assert(strDestination!=NULL && strSource!=NULL);
//2、保护参数
char *pDest = strDestination;
const char *pSrc = strSource;
//3、拷贝数据
while(*pSrc != '\0')
{
*pDest++ = *pSrc++;
}
//4、拷贝结束标记\0
*pDest = '\0';
//5、返回目标指针
return strDestination;
}
strcat
http://www.cplusplus.com/reference/cstring/strcat/
char * strcat ( char * destination , const char * source );
Appends a copy of the source string to the destination string. The terminating null character indestination is overwritten by the first character of source, and a null-character is included at the end ofthe new string formed by the concatenation of both in destination.
源字符串必须以 '\0' 结束。
目标空间必须有足够的大,能容纳下源字符串的内容。
目标空间必须可修改。
字符串自己给自己追加,如何?
char* my_strcat( char *strDestination, const char *strSource )
{
//检查参数有效性
assert(strDestination!=NULL && strSource!=NULL);
//保护参数
char *pDest = strDestination;
const char *pSrc = strSource;
while(*pDest != '\0')
{
pDest++;
}
while(*pSrc != '\0')
{
*pDest++ = *pSrc++;
}
*pDest = *pSrc;
return strDestination;
}
strcmp
http://www.cplusplus.com/reference/cstring/strcmp/
int strcmp ( const char * str1 , const char * str2 );
This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ or until a terminating null-character is reached.
标准规定:
第一个字符串大于第二个字符串,则返回大于0的数字
第一个字符串等于第二个字符串,则返回0
第一个字符串小于第二个字符串,则返回小于0的数字
那么如何判断两个字符串?
int my_strcmp( const char *string1, const char *string2 )
{
assert(string1!=NULL && string2!=NULL);
while(*string1!='\0' || *string2!='\0')
{
if(*string1 - *string2 != 0)
break;
string1++;
string2++;
}
return *string1 - *string2;
}
strncpy
http://www.cplusplus.com/reference/cstring/strncpy/
Copies the first num characters of source to destination. If the end of the source C string (which is signaled by a null-character) is found before num characters have been copied, destination is padded with zeros until a total of num characters have been written to it.
拷贝num个字符从源字符串到目标空间。
如果源字符串的长度小于num,则拷贝完源字符串之后,在目标的后边追加0,直到num个。
char* my_strncpy( char *strDest, const char *strSource, size_t count )
{
assert(strDest!=NULL && strSource!=NULL);
char *pDest = strDest;
const char *pSrc = strSource;
while(count-- != 0)
{
*pDest++ = *pSrc++;
}
return strDest;
}
strncat
http://www.cplusplus.com/reference/cstring/strncat/
Appends the first num characters of source to destination, plus a terminating null-character.
If the length of the C string in source is less than num, only the content up to the terminating null character is copied.
char* my_strncat( char *strDest, const char *strSource, size_t count )
{
assert(strDest!=NULL && strSource!=NULL);
char *pDest = strDest;
const char *pSrc = strSource;
while(*pDest != '\0')
{
pDest++;
}
while(count-- != 0)
{
*pDest++ = *pSrc++;
}
return strDest;
}
strncmp
https://www.cplusplus.com/reference/cstring/strncmp/
int strncmp ( const char * str1 , const char * str2 , size_t num );
比较到出现另个字符不一样或者一个字符串结束或者num个字符全部比较完
int my_strncmp( const char *string1, const char *string2, size_t count )
{
assert(string1!=NULL && string2!=NULL);
while(count-- != 0)
{
if(*string1 - *string2 != 0)
break;
string1++;
string2++;
}
return *string1 - *string2;
strstr
http://www.cplusplus.com/reference/cstring/strstr/
char * strstr ( const char * , const char * );
Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1.
strtok
http://www.cplusplus.com/reference/cstring/strtok/
char * strtok ( char * str , const char * sep );
sep参数是个字符串,定义了用作分隔符的字符集合
第一个参数指定一个字符串,它包含了0个或者多个由sep字符串中一个或者多个分隔符分割的标记。
strtok函数找到str中的下一个标记,并将其用 \0 结尾,返回一个指向这个标记的指针。(注:strtok函数会改变被操作的字符串,所以在使用strtok函数切分的字符串一般都是临时拷贝的内容并且可修改。)
strtok函数的第一个参数不为 NULL ,函数将找到str中第一个标记,strtok函数将保存它在字符串中的位置。
strtok函数的第一个参数为 NULL ,函数将在同一个字符串中被保存的位置开始,查找下一个标记。
如果字符串中不存在更多的标记,则返回 NULL 指针。
strerror
http://www.cplusplus.com/reference/cstring/strerror/?kw=strerror
char * strerror ( int errnum );
返回错误码所对应错误信息。
memcpy
http://www.cplusplus.com/reference/cstring/memcpy/?kw=memcpy
void * memcpy ( void * destination, const void * source, size_t num );
函数memcpy从source的位置开始向后复制num个字节的数据到destination的内存位置。
这个函数在遇到 '\0' 的时候并不会停下来。
如果source和destination有任何的重叠,复制的结果都是未定义的。
void* my_memcpy( void *dest, const void *src, size_t count )
{
assert(dest!=NULL && src!=NULL);
char *pdest = (char *)dest;
const char *psrc = (const char *)src;
if(psrc>=pdest || psrc+count<=pdest)
{
while(count-- != 0)
{
*pdest++ = *psrc++;
}
}
else
{
//内存重叠
pdest = pdest + count - 1;
psrc = psrc + count - 1;
while(count-- != 0)
{
*pdest-- = *psrc--;
}
}
return dest;
}
memmove
http://www.cplusplus.com/reference/cstring/memmove/
void * memmove ( void * destination , const void * source , size_t num );
和memcpy的差别就是memmove函数处理的源内存块和目标内存块是可以重叠的。
如果源空间和目标空间出现重叠,就得使用memmove函数处理。
void * memmove ( void * dst, const void * src, size_t count)
{
void * ret = dst;
if (dst <= src || (char *)dst >= ((char *)src + count)) {
/*
* Non-Overlapping Buffers
* copy from lower addresses to higher addresses
*/
while (count--) {
*(char *)dst = *(char *)src;
dst = (char *)dst + 1;
src = (char *)src + 1;
}
}
else {
/*
* Overlapping Buffers
* copy from higher addresses to lower addresses
*/
dst = (char *)dst + count - 1;
src = (char *)src + count - 1;
while (count--) {
*(char *)dst = *(char *)src;
dst = (char *)dst - 1;
src = (char *)src - 1;
}
}
return(ret);
}
memcmp
http://www.cplusplus.com/reference/cstring/memcmp/
int my_memcmp( const void *buf1, const void *buf2, size_t count )
{
assert(buf1!=NULL && buf2!=NULL);
const char *pbuf1 = (char *)buf1;
const char *pbuf2 = (char *)buf2;
while(count-- != 0)
{
if(*pbuf1 - *pbuf2 != 0)
break;
pbuf1++;
pbuf2++;
}
return *pbuf1 - *pbuf2;
}