目录
1、函数介绍
1.1 strlen
1.2 strcpy
1.3 strcat
1.4 strcmp
1.5 strncpy
1.6 strncat
1.7 strncmp
1.8 strstr
1.9 strtok
1.10 strerror
【补】字符分类函数:
1.11 memcpy
1.12 memmove
1.13 memcmp
1.14 memset
size_t strlen ( const char * str );
- 字符串以 '\0' 作为结束标志,strlen函数返回的是在字符串中 '\0' 前面出现的字符个数(不包含 '\0' )。
- 参数指向的字符串必须要以 '\0' 结束
- 注意函数的返回值为size_t,是无符号的( 易错 )
- 头文件为#include
- 学会strlen函数的模拟实现
#include
#include
int main()
{char ch = 'a';//"abcdef";//C语言没有字符串类型//char arr[] = "abcdef";//6//char arr[10] = { 'a', 'b', 'c', 'd', 'e', 'f'};//6char arr[] = { 'a', 'b', 'c', 'd', 'e', 'f' };//随机int len = strlen(arr);//string lengthprintf("%d\n", len);return 0;
}
#include
#include int main() {if (strlen("abc") - strlen("qwerty") > 0){printf(">\n");}else{printf("<=\n");}return 0; } 输出结果:>
因为strlen函数返回类型是size_t(无符号整型的别称),strlen("abc") - strlen("qwerty")=-3
,-3被当成无符号整型的话,会被编译器当作一个很大很大的数(自然大于零),所以输出 >。改进方法如下(强制类型转换):
#include
#include int main() {if ((int)strlen("abc") - (int)strlen("qwerty") > 0){printf(">\n");}else{printf("<=\n");}return 0; }
模拟实现strlen:
法一:计数器
#include
#include size_t my_strlen(const char* str) {assert(str);int count = 0;while (*str){count++;str++;}return count; }int main() {char arr[] = "abcdef";int len = my_strlen(arr);printf("%d\n", len);return 0; } 法二:递归
#include
#include size_t my_strlen(const char* str) {assert(str);if (*str == '\0'){return 0;}else{return 1 + my_strlen(str + 1);} }int main() {char arr[] = "abcdef";int len = my_strlen(arr);printf("%d\n", len);return 0; } 法三:指针-指针
#include
#include size_t my_strlen(const char* str) {assert(str);const char* eos = str;while (*eos++){;}return(eos - str - 1); }int main() {char arr[] = "abcdef";int len = my_strlen(arr);printf("%d\n", len);return 0; }
char* strcpy(char * destination, const char * source );
#include
#include int main() {//char arr1[4] = "x";char* arr1 = "qwertyuiop";//arr1指向的是常量字符串,常量是不可修改的char arr2[] = "abcdef";//char arr2[10] = { 'a', 'b', 'c'};strcpy(arr1, arr2);printf("%s\n", arr1);return 0; } //程序会崩溃 //strcpy函数返回的是目标空间的起始地址 //strcpy函数的返回类型的设置是为了实现链式访问
模拟实现strcpy:
#include
#include char* my_strcpy(char* dest, const char* src) {assert(src && dest);char* ret = dest;while(*dest++ = *src++){;}return ret; }int main() {char arr1[20] = { 0 };char* arr2 = "hello c";printf("%s\n", my_strcpy(arr1, arr2));return 0; }
char * strcat ( char * destination, const char * source );
模拟实现strcat:
#include
#include char* my_strcat(char* dest, char* src) {assert(dest && src);char* ret = dest;//找目标空间中的\0while (*dest){dest++;}//拷贝while (*dest++ = *src++){;}return ret; }int main() {char arr1[20] = "hello";char arr2[] = " c";printf("%s\n", my_strcat(arr1, arr2));return 0; }
int strcmp ( const char * str1, const char * str2 );
模拟实现strcmp:
#include
#include int my_strcmp(const char* s1, const char* s2) {assert(s1 && s2);while (*s1 == *s2){if (*s1 == '\0'){return 0;//相等}s1++;s2++;}//不相等if (*s1 > *s2)return 1;elsereturn -1; }int main() {char arr1[] = "abcd";char arr2[] = "abdc";int ret = my_strcmp(arr1, arr2);if (ret >0){printf(">\n");}else if (ret == 0){printf("== \n");}else{printf("<\n");}printf("%d\n", ret);// 1 0 -1//strcmp函数比较的不是字符串的长度!!!!//而是比较字符串中对应位置上的字符的大小,如果相同,就比较下一对儿,直到不同或者都遇到\0return 0; }
char * strncpy ( char * destination, const char * source, size_t num );
#include
#include
#include int main()
{char arr1[] = "abcdef";char arr2[] = "qwe";strncpy(arr1, arr2, 5);printf("%s\n", arr1);return 0;
}
char * strncat ( char * destination, const char * source, size_t num );
#include
#include #include int main() {char arr1[20] = "abcdef\0XXXXXXXX";char arr2[] = "qwe";strncat(arr1, arr2, 5);printf("%s\n", arr1);return 0; }
int strncmp ( const char * str1, const char * str2, size_t num );
#include
#include #include int main() {char arr1[] = "abcdef";char arr2[] = "abcdq";int ret = strncmp(arr1, arr2, 4);printf("%d\n", ret);return 0; }
char * strstr ( const char *str1, const char * str2);
模拟实现strstr:
#include
#include char* my_strstr(const char* str1, const char* str2) {assert(str1 && str2);const char* s1 = str1;const char* s2 = str2;const char* cur = str1;while (*cur){s1 = cur;s2 = str2;while (*s1 && *s2 && (*s1 == *s2)){s1++;s2++;}if (*s2 == '\0'){return (char*)cur;}cur++;}return NULL;//找不到 }// //查找子串的时候 //KMP算法---更高效 //int main() {char arr1[] = "abbbcdef";char arr2[] = "bbc";char* ret = my_strstr(arr1, arr2);if (NULL == ret){printf("找不到子串\n");}else{printf("%s\n", ret);}return 0; }
char * strtok ( char * str, const char * sep );
#include
#include int main() {char arr[] = "ttao@yeah.net.hehe@haha nihao";char buf[50] = { 0 };//ttao@yeah.net.hehe@haha nihaostrcpy(buf, arr);const char* sep = "@.";//strtok//"@."printf("%s\n", strtok(buf, sep));//只找第一个标记printf("%s\n", strtok(NULL, sep));//是从保存的好的位置开始继续往后找printf("%s\n", strtok(NULL, sep));//是从保存的好的位置开始继续往后找return 0; } 如果分割的部分有很多,printf("%s\n", strtok(NULL, sep));造成了代码冗余,做出以下优化(for循环):
#include
#include int main() {char arr[] = "ttao@yeah.net.hehe@haha nihao";char buf[50] = { 0 };//ttao@yeah.net.hehe@haha nihaostrcpy(buf, arr);const char* sep = "@.";//strtok//"@."char* str = NULL;for (str=strtok(buf, sep); str!=NULL; str=strtok(NULL, sep)){printf("%s\n", str);}return 0; }
char * strerror ( int errnum );
返回错误码,所对应的错误信息。
我们经常上网查找一些资源,对下面网页图片,我想大多数人都不会陌生:
404 not found,是HTTP对网页错误情况返回的一种状态码,当用户在浏览器中输入网址时,服务器会根据输入的地址判断是否有对应的网页信息,如果没有对应信息,说明用户输入的可能是一串无效的链接,服务器就会向用户返回404 not found状态码,告诉用户没有找到对应的网页信息。类似的,strerror这个函数在编程的时候也会返回一些错误代码,这些错误数字都代表了某种特殊含义:
#include
#include #include //errno相当于全局变量int main() {printf("%s\n", strerror(0));printf("%s\n", strerror(1));printf("%s\n", strerror(2));printf("%s\n", strerror(3));printf("%s\n", strerror(4));printf("%s\n", strerror(5));printf("%s\n", strerror(6));printf("%s\n", strerror(7));printf("%s\n", strerror(8));return 0; }
当然了,还有更多,这些只是列举部分典例。
#include
#include #include //errno相当于全局变量 #include //malloc头文件 #include //INT_MAX头文件 int main() {int* p = (int*)malloc(INT_MAX);//向堆区申请内存的if (p == NULL){printf("%s\n", strerror(errno));return 1;}return 0; } 运行结果:
函数 | 如果他的参数符合下列条件就返回真 |
iscntrl | 任何控制字符 |
isspace | 空白字符:空格‘ ’,换页‘\f’,换行'\n',回车‘\r’,制表符'\t'或者垂直制表符'\v' |
isdigit | 十进制数字 0~9 |
isxdigit | 十六进制数字,包括所有十进制数字,小写字母a~f,大写字母A~F |
islower | 小写字母a~z |
isupper | 大写字母A~Z |
isalpha | 字母a~z或A~Z |
isalnum | 字母或者数字,a~z,A~Z,0~9 |
ispunct | 标点符号,任何不属于数字或者字母的图形字符(可打印) |
isgraph | 任何图形字符 |
isprint | 任何可打印字符,包括图形字符和空白字符 |
下面简单的举几个例子:
判断是否是小写字母:islower
#include
#include #include int main() {char ch = 'A';char sh = 'a';int ret = islower(ch);int rot = islower(sh);printf("%d\n", ret);printf("%d\n", rot);return 0; } 转换小写:tolower
转换大写:toupper
#include
#include #include #include int main() {char ch = 'A';putchar(toupper(ch));printf("\n");putchar(tolower(ch));return 0; }
void * memcpy ( void * destination, const void * source, size_t num );
模拟实现memcpy:
#include
#include #include void* my_memcpy(void* dest, const void* src, size_t count) {assert(dest && src);void* ret = dest;while (count--){*(char*)dest = *(char*)src;dest = (char*)dest + 1;src = (char*)src + 1;}return ret; }int main() {int arr1[10] = { 1,2,3,4,5,6,7,8,9,10 };int arr2[5] = { 0 };my_memcpy(arr2, arr1, 20);int i = 0;int sz = sizeof(arr2) / sizeof(arr2[0]);for (i = 0; i < sz; i++){printf("%d ", arr2[i]);}return 0; }
void * memmove ( void * destination, const void * source, size_t num );
如果source和destination有任何的重叠,显然上述模拟代码有问题,这个时候就需要用到memmove函数:
#include
#include #include void* my_memmove(void* dest, const void*src, size_t count) {assert(dest && src);void* ret = dest;//1if (dest < src){//前->后while (count--){*(char*)dest = *(char*)(src);dest = (char*)dest + 1;src = (char*)src + 1;}}else{//后->前while (count--){*((char*)dest+count) = *((char*)src + count);}}return ret; }int main() {int arr1[10] = { 1,2,3,4,5,6,7,8,9,10 };// 1 2 1 2 3 4 5 8 9 10//memcpy(arr1+2, arr1, 20);//errormy_memmove(arr1+2, arr1, 20);//可以实现重叠内存的拷贝int i = 0;int sz = sizeof(arr1) / sizeof(arr1[0]);for (i = 0; i < sz; i++){printf("%d ", arr1[i]);}return 0; }
int memcmp ( const void * ptr1, const void * ptr2, size_t num );
#include
#include int main()
{int arr1[] = { 1,2,3,4,5 };int arr2[] = { 1,2,3,4,0x11223305 };int ret = memcmp(arr1, arr2, 18);printf("%d\n", ret);return 0;
}
#include
#include int main()
{int arr[] = { 0x11111111,0x22222222,3,4,5 };memset(arr, 6, 20);//memset是以字节为单位来初始化内存单元的return 0;
}
下一篇:31.项目部署