文章目录
字符串函数
1. 函数介绍
1.1 strlen
size_t strlen ( const char * str );
- 字符串以
'\0'
作为结束标志,返回的是字符串'\0'
前面字符的个数(不包括'\0'
)。 - 参数指向的字符串必须以
'\0'
结束。 - 函数的返回值是
size_t
,是无符号的(易错)。
注意:
#include <stdio.h>
#include <string.h>
int main()
{
const char* str1 = "abcdef";//6
const char* str2 = "bbb";//3
if (strlen(str2) - strlen(str1) > 0)
{
printf("str2>str1\n");
}
else
{
printf("srt1>str2\n");
}
printf("%d\n", strlen(str1));
printf("%d\n", strlen(str2));
return 0;
}
我们知道,字符串str1的长度是6,字符串str2的长度是3。按理说:(3-6)<0,打印结果应该是str1>str2.
但实际结果却是:
为什么呢?
注意上述strlen函数介绍中的第3条,strlen
函数的返回值是size_t
,是无符号的,所以,strlen(str2)-strlen(str1)
的结果就是4294967293
>0。
1.2 strcpy
char* strcpy(char * destination, const char * source );
- Copies the C string pointed by source into the array pointed by destination, including theterminating null character (and stopping at that point).
- 原字符串必须以
'\0'
结束。 - 会将原字符串中的
'\0'
拷贝到目标空间。 - 目标空间必须足够大,以确保能够放下原字符串。
- 目标空间必须可变,不能被
const
修饰。
例子:
int main()
{
char str1[] = "abcdef";
char str2[20] = { 0 };
strcpy(str2, str1);
printf("%s\n", str2);
return 0;
}
1.3 strcat
char * strcat ( char * destination, const char * source );
- Appends a copy of the source string to the destination string. The terminating null characterin destination is overwritten by the first character of source, and a null-character is includedat the end of the new string formed by the concatenation of both in destination.
- 原字符串必须以
'\0'
结束。 - 目标空间必须足够大,以确保能够放下原字符串。
- 目标空间必须可修改,不能被
const
修饰。 - 注意:不能给自己追加。
例子:
int main()
{
char str1[20] = "abcdef";
strcat(str1, "gh");
printf(str1);
return 0;
}
1.4 strcmp
int strcmp ( const char * str1, const char * str2 );
- This function starts comparing the first character of each string. If they are equal to eachother, it continues with the following pairs until the characters differ or until a terminatingnull-character is reached.
- 标准规定:
第一个字符串大于第二个字符串,则返回大于0的数字。
第一个字符串等于第二个字符串,则返回0。
第一个字符串小于第二个字符串,则返回小于0的数字。
1.5 strncpy
char * strncpy ( char * destination, const char * source, size_t num );
与strcpy类似,只是增加了拷贝字符的个数限制。
- 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个。
1.6 strncat
char * strncat ( char * destination, const char * source, size_t num );
与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 terminatingnull-character is copied.
1.7 strncmp
int strncmp ( const char * str1, const char * str2, size_t num );
- 比较到出现一个字符不一样或者一个字符串结束或者num个字符全部比较完。
//example:
int main()
{
char arr1[] = "abcdef";
char arr2[] = "abc";
int ret = strncmp(arr1, arr2, 3);//比较3个字符
if (ret == 0)
printf("==\n");
else if (ret < 0)
printf("<\n");
else
printf(">\n");
return 0;
}
1.8 strstr
char * strstr ( const char *str1, const char * str2);
- Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part ofstr1.
- 在字符串str1中查找是否包含字符串str2。如果有,返回字符串str1中str2的起始地址;否则,返回空指针。
//example:
int main()
{
char str[] = "This is a simple string";
char* pch;
pch = strstr(str, "simple");
strncpy(pch, "helloo", 6);
puts(str);
return 0;
}
1.9 strtok
char * strtok ( char * str, const char * sep );
字符串分割函数。
- sep参数是个字符串,定义了用作分隔符的字符集合.
- 第一个参数指定一个字符串,它包含了0个或者多个由
sep
字符串中一个或者多个分隔符分割的标记。 strtok
函数找到str中的下一个标记,并将其用\0
结尾,返回一个指向这个标记的指针。(注:strtok
函数会改变被操作的字符串,所以在使用strtok
函数切分的字符串一般都是临时拷贝的内容并且可修改。)- 首次使用
strtok
函数,strtok
函数的第一个参数不为NULL
,函数将找到str
中第一个标记,strtok
函数将保存它在字符串中的位置。 - 再次使用
strtok
函数,strtok
函数的第一个参数为NULL
,函数将在同一个字符串中被保存的位置开始,查找下一个标记。以后再使用,则与此类似。 - 如果字符串中不存在更多的标记,则返回
NULL
指针。
//example:
int main()
{
const char* sep = "@.";
char arr1[] = "hello@world.com";
char cp[20] = { 0 };
strcpy(cp, arr1);
char* ret = strtok(cp, sep);
printf("%s\n",ret);//hello
ret = strtok(NULL, sep);
printf("%s\n", ret);//world
ret = strtok(NULL, sep);
printf("%s\n", ret);//com
ret = strtok(NULL, sep);
printf("%s\n", ret);//(NULL)
return 0;
}
1.10 strerror
char * strerror ( int errnum );
返回错误码所对应的错误信息。
//example:
#include <stdio.h>
#include <string.h>
#include <errno.h>//必须包含的头文件
int main()
{
FILE* pFile;
pFile = fopen("unexist.ent", "r");
if (pFile == NULL)
printf("Error opening file unexist.ent: %s\n", strerror(errno));
//errno: C语言设置的一个全局的错误码存放的变量
return 0;
}
1.11 字符分类函数
函数 | 如果它的参数符合下列条件就返回真 |
---|---|
iscntrl | 任何控制字符 |
isspace | 空白字符:空格‘ ’,换页‘\f’,换行’\n’,回车‘\r’,制表符’\t’或者垂直制表符’\v’ |
isdigit | 十进制数字 0~9 |
isxdigit | 十六进制数字,包括所有十进制数字,小写字母af,大写字母AF |
islower | 小写字母a~z |
isupper | 大写字母A~Z |
isalpha | 字母a ~ z 或 A ~ Z |
isalnum | 字母或者数字,az,AZ,0~9 |
ispunct | 标点符号,任何不属于数字或者字母的图形字符(可打印) |
isgraph | 任何图形字符 |
isprint | 任何可打印字符,包括图形字符和空白字符 |
字符转换:
int tolower ( int c );
int toupper ( int c );
//example:
int main()
{
int i = 0;
char str[] = "Test String.\n";
char c;
while (str[i])
{
c = str[i];
if (isupper(c))
c = tolower(c);
putchar(c);
i++;
}
return 0;
}
1.12 memcpy
void * memcpy ( void * destination, const void * source, size_t num );
- 函数memcpy从source的位置开始向后复制
num
个字节的数据到destination
的内存位置。 - 这个函数在遇到
'\0'
的时候并不会停下来。 - 如果source和destination有任何的重叠,复制的结果都是未定义的。
//example:
int main()
{
int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
int arr2[12] = { 0 };
memcpy(arr2, arr1, 40);
char str1[] = "abcdef\0h";
char str2[10] = "sssssss";
memcpy(str2, str1, 10);
return 0;
}
1.13 memmove
void * memmove ( void * destination, const void * source, size_t num );
- 和memcpy的差别就是memmove函数处理的源内存块和目标内存块是可以重叠的。
- 若原空间和目标空间出现重叠,就得使用memmove处理。
//example
int main()
{
char str[] = "memmove can be very useful......";
memmove(str + 20, str + 15, 11);
puts(str);
return 0;
}
1.14 memcmp
int memcmp ( const void * ptr1, const void * ptr2, size_t num );
- 比较从ptr1和ptr2指针开始的num个字节.
- 返回值:
//example:
int main()
{
int arr1[] = { 1,2,3,4,5,6 };
int arr2[] = { 1,3,2 };
int ret = memcmp(arr1, arr2, 12);//<
if (ret == 0)
printf("==\n");
else if (ret < 0)
printf("<\n");
else
printf(">\n");
return 0;
}
2. 库函数的模拟实现
废话不多少,直接看代码:
2.1 模拟实现strlen
//code-1 循环
int my_strlen(const char* str)
{
assert(str);
int count = 0;
while (*str++)
{
count++;
}
return count;
}
//code-2 递归
int my_strlen(const char* str)
{
assert(str);
if (*str != '\0')
return 1 + my_strlen(str + 1);
else
return 0;
}
//code-3 指针相减
int my_strlen(const char* str)
{
assert(str);
const char* left = str;
const char* right = left;
while (*right != '\0')
{
right++;
}
return right - left;//指针相减 得到的是 指针间元素的个数
}
2.2 模拟实现strcpy
char* my_strcpy(char* arr1, const char* arr2)
{
assert(arr1 != NULL);
assert(arr2 != NULL);
char* ret = arr1;
while (*arr1++ = *arr2++);
return ret;
}
2.3 模拟实现strcat
char* my_strcat(char* dest, const char* sor)
{
assert(dest && sor);
char* str = dest;
while (*str != '\0')
{
str++;
}
while (*str++ = *sor++);
return dest;
}
2.4 模拟实现strstr
char* my_strstr(const char* str1, const char* str2)
{
assert(str1 && str2);
const char* s1 = str1;
const char* s2 = str2;
const char* p = s1;//记录位置
while (*p)
{
s1 = p;
s2 = str2;
while (*s1 == *s2 && *s1 != '\0' && *s2 != '\0')
{
s1++;
s2++;
}
if (*s2 == '\0')
{
return (char*)p;
}
p++;
}
return NULL;
}
2.5 模拟实现strcmp
int my_strcmp(const char* str1, const char* str2)
{
assert(str1 && str2);
while (*str1 == *str2)
{
if (*str1 == '\0')
return 0;
str1++;
str2++;
}
return (*str1 - *str2);
}
2.6 模拟实现memcpy
void* my_memcpy(void* dest, const void* src, size_t num)
{
assert(dest && src);
void* ret = dest;
while (num--)
{
(char*)dest = (char*)src;
dest = (char*)dest + 1;
src = (char*)src + 1;
}
return ret;
}
2.7 模拟实现memmove
void* my_memmove(void* dest, const void* src, size_t num)
{
assert(dest && src);
void* ret = dest;
//前->后
if (dest < src)
{
while (num--)
{
*(char*)dest = *(char*)src;
dest = (char*)dest + 1;
src = (char*)src + 1;
}
}
else//后->前
{
while (num--)
{
*((char*)dest + num) = *((char*)src + num);
}
}
return ret;
}