9_字符函数和字符串函数
一.内容
-
- 字符串函数
-
- 字符函数
-
- 内存函数
二.函数介绍
1. strlen <string.h>
size_t strlen(const char * str);
- 字符串以’\0’作为结束标准.strlen函数的返回的是在字符串中’\0’前面出现的字符个数(不包含’\0’)
- 参数指向的字符串必须要以 ‘\0’ 结束。
- 注意函数的返回值为size_t,是无符号的( 易错 )
- 学会strlen函数的模拟实现
#include<stdio.h>
//法一:计数器
int my_strlen(const char* str)
{
int count = 0;
while (*str)
{
count++;
str++;
}
return count;
}
int main()
{
char arr[] = "abcdef";
int len = my_strlen(arr);
printf("%d", len);
return 0;
}
//法二:函数递归
int my_strlen(const char* str)
{
if (*str != '\0')
{
return 1 + my_strlen(str + 1);
}
else
{
return 0;
}
}
int main()
{
char arr[] = "abcdef";
int len = my_strlen(arr);
printf("%d", len);
return 0;
}
//法三:指针减指针
int my_strlen(const char* str)
{
const char* start = str;//字符串的起始位置就是str
const char* end = str;
while (*end != '\0')
{
end++;//用来找到字符串的末尾处
}
return end - start;
}
int main()
{
char arr[] = "abcdef";
int len = my_strlen(arr);
printf("%d", len);
return 0;
}
- 注:两个无符号数相减得到的还是无符号数
2. strcpy <string.h>
char* strcpy(char * destination,const char * source);
- Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point)
- 源字符串必须以’\0’结束
没有\0会一直拷贝导致越界
-
会将源字符串中的’\0’拷贝到目标空间
-
目标空间必须足够大,以确保能存放源字符串
-
目标空间必须可变
-
学会模拟实现
#include<stdio.h>
#include<string.h>
//法一
void my_strcpy(char* est, const char* src)
{
while (*src != '\0')
{
*est = *src;
est++;
src++;
}
*est = '\0';
}
int main()
{
char arr1[20] = { 0 };
char arr2[] = "abcdef";
my_strcpy(arr1, arr2);
printf("%s\n", arr1);
return 0;
}
//法二
#include<stdio.h>
#include<string.h>
#include<assert.h>
//strcpy函数返回的是目标空间的起始地址
char* my_strcpy(char* dest, const char* src)
{
char* ret = dest;
assert(dest && src);
while (*dest++ = *src++)
{
;
}
return ret;
}
int main()
{
char arr1[20] = { 0 };
char arr2[] = "abcdef";
//my_strcpy(arr1, arr2);
//printf("%s\n", arr1);
//或者直接放printf里面
printf("%s\n", my_strcpy(arr1, arr2));
return 0;
}
3. strcat <string.h>
char * strcat(char * destination ,const char * source);
-
Appends a copy of the source string to the destination string. The terminating null character in destination is overwritten by the first character of source, and a null-character is included at the end of the new string formed by the concatenation of both in destination.
-
源字符串必须以’\0’结束
-
目标空间必须足够大,能容纳下源字符串的内容
-
目标空间必须可修改
-
字符串给自己追加,如何?
//模拟strcat
#include<stdio.h>
#include<assert.h>
//strcat返回目标空间起始地址
char* my_strcat(char* dest, const char* src)
{
assert(dest && src);
char* ret = dest;
//1.找到目标文件的末尾
while (*dest != '\0')
{
dest++;
}
//2.数据追加
while (*dest++ = *src++)
{
;
}
return ret;
}
int main()
{
char arr1[20] = "abc";
char arr2[] = "def";
my_strcat(arr1, arr2);
printf("%s", arr1);
return 0;
}
4. strcmp <string.h>
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的数字
//模拟strcmp
#include<stdio.h>
int my_strcmp(const char* str1, const char* str2)
{
while (*str1 == *str2)
{
if (*str1 == '\0')
return 0;
str1++;
str2++;
}
if (*str1 > *str2)
return 1;
else
return -1;
}
int main()
{
char arr1[] = "abcdef";
char arr2[] = "abq";
int ret = my_strcmp(arr1, arr2);
printf("%d\n", ret);
return 0;
}
5. strncpy <string.h>
char * strncpy (char * destination, const char * source,size_t num);
-
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个字符从源字符串到目标空间(让拷几个就拷几个,不会自动将后面’\0’拷进来)
-
如果字符串长度小于num,则拷完源字符串之后,在目标后面追加0,直到num个
6. strncat <string.h>
char * strncat ( char * destination, const char * source, size_t num);
- Appends the first num characters of source to destination, plus a terminating null-character.
(追加num个字符后再加一个’\0’)
- If the length of the C string in source is less than num, only the content up to the terminating null-character is copied.
(如果num大于源字符串长度,则追加到’\0’后停止)
7. strncmp <string.h>
int strncmp( const char * str1,cont char * str2,size_t num);
8. strstr <string.h>
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 of str1.
//模拟strstr
#include<stdio.h>
#include<assert.h>
const char* my_strstr(const char* str1, const char* str2)
{
const char* cp;//记录开始匹配的位置
const char* s1;//遍历str1指向的字符串
const char* s2;//遍历str2指向的字符串
assert(str1 && str2);
if (*str2 == '\0')
return str1;
cp = str1;
while (*cp)
{
s1 = cp;
s2 = str2;
while (*s1 && *s2 && *s1 == *s2)
{
s1++;
s2++;
}
if (*s2 == '\0')
return cp;
cp++;
}
return NULL;
}
int main()
{
char arr1[] = "abbcdef";
char arr2[] = "bbc";
const char* ret = my_strstr(arr1, arr2);
if (ret == NULL)
{
printf("找不到\n");
}
else
{
printf("%s\n", ret);
}
return 0;
}
9. strtok <string.h>
char * strtok ( char * str , const char * sep);
-
sep参数是个字符串,定义了用作分隔符的字符串集合
-
第一个参数指定一个字符串,它包含了0个或者多个由sep字符串中一个或者多个分隔符分割的标记.
-
strtok函数找到str中的下一个标记,并将其用’\0’结尾,返回这个标记的指针.(注:strtok会改变被操作的字符串,所以strtok函数切分的字符串一般都是临时拷贝的内容并且可以修改.)
-
strtok函数的第一个参数不为NULL,函数将找到str中第一个标记,strtok函数将保存它在字符串中的位置
-
strtok函数的第一个参数位NULL,函数将在同一个字符串中被保存的位置开始,查找下一个标记.
-
如果字符串不存在更多标记,则返回NULL指针.
#include <stdio.h>
int main()
{
char *p = "xiaotangxuedaima@bitedu.tech";
const char* sep = ".@";
char arr[30];
char *str = NULL;
strcpy(arr, p);//将数据拷贝一份,处理arr数组的内容
for(str=strtok(arr, sep); str != NULL; str=strtok(NULL, sep))
{
printf("%s\n", str);
}
}
10. strerror <string.h>
char * strerror ( int errnum );
- strerror是将错误码翻译成错误信息,返回错误信息的起始地址!
使用库函数还是进行正规的软件设计 会产生- 错误码
404-错误码
- 补充:perror
11. 字符分类函数
- <string.h>
12. 字符转换
int tolower( int c );
int toupper( int c );
/* isupper example */
#include <stdio.h>
#include <ctype.h>
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;
}
13. memcpy <string.h>
void * memcpy ( void * destination ,const void * source, size_t num );
- 函数memcpy从source的位置开始向后复制num个字节的数据到destination的内存位置
- 这个函数在遇到’\0’的时候会停下来
- 如果source和destination有任何的重合,复制结果都是未定义的
//模拟memcpy
#include<stdio.h>
#include<assert.h>
void* my_memcpy(void* dest, const void* src, int sz)
{
void* ret = dest;
assert(dest && src);
while (sz--)
{
*(char*)dest = *(char*)src;
dest = (char*)dest + 1;
src = (char*)src + 1;
//注意
//++(char*)dest//可能会存在无法编译的问题
//++(char*)src//可能会存在无法编译的问题
//上面两行代码C语言中可以运行,C++不行
//后置加加在任何地方都不能运行
}
return ret;
}
int main()
{
int arr1[20] = { 0 };
int arr2[] = { 1,2,3,4,5 };
my_memcpy(arr1, arr2, 20);
return 0;
}
14. memmove <string.h>
void * memmove ( void * destination, const void * source,size_t num);
- 和memcpy的差别就是memmove函数处理的源内存块和目标内存块是可以重叠的(mememove重不重叠都可以拷贝)
- 如果源空间和目标空间出现重叠,就得使用memmove函数处理
模拟memmove
#include<stdio.h>
#include<assert.h>
void* my_memmove(void* dest, const void* src, int sz)
{
void* ret = dest;
assert(dest && src);
if (dest < src)
{
//前->后
while (sz--)
{
*(char*)dest = *(char*)src;
dest = (char*)dest + 1;
src = (char*)src + 1;
}
}
else
{
//后->前
while (sz--)
{
*((char*)dest + sz)= *((char*)src + sz);
}
}
return ret;
}
int main()
{
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
my_memmove(arr+2, arr , 20);
int i = 0;
for (i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
{
printf("%d ", arr[i]);
}
return 0;
}
15. memset <string.h>
void * memset ( void * ptr,int value,size_t num);
- 使用
16. memcmp <string.h>
int memcmp ( const void* ptr1, const void* ptr2,size_t num);
- 比较从ptr1和ptr2指针开始的num个字节
- 返回值如下: