时间:2023-01-06 09:04:08 | 栏目:C代码 | 点击:次
功能:求字符串长度
#include<stdio.h> #include<string.h> int main() { printf("%d\n", strlen("abcdef"));//6 return 0; }
注意事项:
注:
#include<stdio.h> #include<string.h> int main() { if (strlen("abc") - strlen("abcdef") > 0) printf(">"); else printf("<="); return 0; }
解析:
size_t strlen ( const char * str );
法一:计数器法:
#include <stdio.h> #include<assert.h> int my_strlen(const char* str) { assert(str); int count = 0; while (*str != '\0')//判断字符串是否结束 { count++; str++; } return count; } int main() { int len = my_strlen("abcdef"); printf("%d\n", len); // 6 return 0; }
法二:递归法:
#include<stdio.h> int my_strlen(char* s) { if (*s == '\0') return 0; else return 1 + my_strlen(s + 1); } int main() { char arr[] = "abcdef"; int len = my_strlen(arr); printf("%d\n", len); // 6 return 0; }
法三:指针-指针:
#include<stdio.h> int my_strlen(char* s) { char* p = s; while (*p != '\0') p++; return p - s; } int main() { char arr[] = "abcdef"; int len = my_strlen(arr); //6 printf("%d\n", len); return 0; }
功能:拷贝字符串
#include<stdio.h> #include<string.h> int main() { char arr1[] = "abcdef"; //char arr1[] = { 'a','b','c','e','f','\0' }; char arr2[20] = "xxxxxxxxxxx"; strcpy(arr2, arr1); //此时arr2="abcdef\0xxxx" printf("%s\n", arr2); // abcdef return 0; }
strcpy函数的第一个参数放目的地arr2,第二个参数放源字符串数据arr1。把arr1的内容拷到arr2上。
注意事项:
#include<stdio.h> #include<assert.h> char* my_strcpy(char* dest, const char* src) { char* ret = dest; assert(dest && src); while (*dest++ = *src++) { ; } return ret; } int main() { char arr1[] = {'a', 'b', 'c', 'd', 'e', 'f', '\0'}; char arr2[20] = "xxxxxxxxxxxx"; my_strcpy(arr2, arr1); printf("%s\n", arr2); // abcdef return 0; }
功能:连接字符串
#include<stdio.h> #include<string.h> int main() { char arr1[30] = "hello"; char arr2[] = "world";// {'w', 'o', 'r', 'l', 'd', '\0'}; strcat(arr1, arr2); printf("%s\n", arr1);// helloworld return 0; }
把arr2的字符追加到arr1上去。
注意事项:
#include<stdio.h> #include<assert.h> char* my_strcat(char* dest, const char* src) { char* ret = dest; assert(dest && src); //1. 目标空间中的\0 while (*dest) { dest++; } //2. 追加内容到目标空间 while (*dest++ = *src++) { ; } return ret; } int main() { char arr1[30] = "hello"; char arr2[] = "world";// {'w', 'o', 'r', 'l', 'd', '\0'}; printf("%s\n", my_strcat(arr1, arr2)); // helloworld return 0; }
功能:字符串比较
strcmp是比较的是对应位置上的字符ASCII大小,而不是字符串长度。
#include<stdio.h> #include<string.h> int main() { char arr1[] = "abcdef"; char arr2[] = "bbq"; int ret = strcmp(arr1, arr2); printf("%d\n", ret); // -1 return 0; }
注意:
#include<stdio.h> #include<assert.h> 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; /*if (*str1 > *str2) return 1; else return -1;*/ } int main() { char arr1[] = "abc"; char arr2[] = "abc"; int ret = my_strcmp(arr1, arr2); /*printf("%d\n", ret);*/ if (ret<0) { printf("arr1<arr2"); } else if (ret >0) { printf("arr1>arr2"); } else { printf("arr1==arr2"); } return 0; }
功能:拷贝指定元素个数的函数
#include<stdio.h> #include<string.h> int main() { char arr1[] = "xxxxxxxxxxxxxxxx"; char arr2[] = "hello bit"; strncpy(arr1, arr2, 5); printf("%s\n", arr1); //helloxxxxxxxxxxx }
如下:
int main() { char arr1[] = "xxxxxxxxxxxxxxxx"; char arr2[] = "he"; strncpy(arr1, arr2, 5); printf("%s\n", arr1); //he\0\0\0 ---》he }
#include<stdio.h> #include<assert.h> char* my_strncpy(char* dest, const char* str, size_t n) { assert(dest && str); char* ret = dest; while (n--) { *dest++ = *str++; } return ret; } int main() { char arr1[] = "xxxxxxxxxx"; char arr2[] = "abcde"; printf("%s\n", my_strncpy(arr1, arr2, 4)); // abcdxxxxxx return 0; }
#include<stdio.h> #include<string.h> int main() { char arr1[20] = "hello"; char arr2[] = "world"; printf("%s\n", strncat(arr1, arr2, 3)); //hellowor return 0; }
注意:
strncat追加后,会主动在追加后放一个 '\0' 进去,确保其是个字符串。
#include<stdio.h> #include<assert.h> char* my_strncat(char* dest, const char* str, size_t n) { assert(dest && str); char* ret = dest; while (*dest) { dest++; } while (n--) { *dest++ = *str++; } *dest = '\0'; return ret; } int main() { char arr1[20] = "hello\0xxxxx"; char arr2[] = "bitxxxxx"; printf("%s\n", my_strncat(arr1, arr2, 3)); //hellobit return 0; }
功能:实现指定位置的字符数比较函数
#include<stdio.h> #include<string.h> int main() { char arr1[] = "abcdef"; char arr2[] = "abcqqqqq"; printf("%d\n", strncmp(arr1, arr2, 4));//-1 printf("%d\n", strncmp(arr1, arr2, 3));//0 return 0; }
#include<stdio.h> #include<assert.h> int my_strncmp(char* dest, const char* str, size_t n) { int ret = 0; assert(dest && str); while (n && !(*dest - *str)) { n--; dest++; str++; } if (n && *dest - *str > 0) return 1; else if (n && *dest - *str < 0) return -1; return ret; } int main() { char arr1[] = "abcdef"; char arr2[] = "abcqqqqq"; printf("%d\n", my_strncmp(arr1, arr2, 3)); //0 return 0; }
功能:判断一个字符串是否为另一字符串的子集,若是,返回从第一个相等一直到末尾
#include<stdio.h> #include<string.h> int main() { char arr1[] = "abbbcdef"; char arr2[] = "bbc"; char* ret = strstr(arr1, arr2); if (NULL == ret) printf("没找到\n"); else printf("%s\n", ret); // bbcdef return 0; }
#include<stdio.h> #include<assert.h> char* my_strstr(const char* str, const char* substr) { const char* s1 = str; const char* s2 = substr; const char* cur = str; assert(str && substr); if (*substr == '\0') { return (char*)str; } while (*cur) { s1 = cur; s2 = substr; while (*s1 && *s2 && *s1 == *s2) { s1++; s2++; } if (*s2 == '\0') return (char*)cur; cur++; } return NULL; } int main() { char arr1[] = "abbbcdef"; char arr2[] = "bbcq"; 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 <stdio.h> #include <string.h> int main() { const char* p = "@.#,"; char arr[] = "en@yu.xia#sh,ge"; char buf[50] = { 0 };// "en@yu.xia#sh,ge"; strcpy(buf, arr); /*char* str = NULL; for (str = strtok(buf, p); str != NULL; str=strtok(NULL, p)) { printf("%s\n", str); }*/ char* str = strtok(buf, p); printf("%s\n", str); str = strtok(NULL, p);//en printf("%s\n", str); str = strtok(NULL, p);//yu printf("%s\n", str); str = strtok(NULL, p);//sh printf("%s\n", str); str = strtok(NULL, p);//ge printf("%s\n", str); //strtok - 开始返回NULL return 0; }
功能:把错误码翻译成错误信息
C语言中‘,规定了一些信息,错误码 - 错误信息
#include <stdio.h> #include <string.h> int main() { const char* p = "@.#,"; char arr[] = "en@yu.xia#sh,ge"; char buf[50] = { 0 };// "en@yu.xia#sh,ge"; strcpy(buf, arr); /*char* str = NULL; for (str = strtok(buf, p); str != NULL; str=strtok(NULL, p)) { printf("%s\n", str); }*/ char* str = strtok(buf, p); printf("%s\n", str); str = strtok(NULL, p);//en printf("%s\n", str); str = strtok(NULL, p);//yu printf("%s\n", str); str = strtok(NULL, p);//sh printf("%s\n", str); str = strtok(NULL, p);//ge printf("%s\n", str); //strtok - 开始返回NULL return 0; }
用途示例:
C语言可以操作文件,打开文件 - fopen
当库函数使用的时候,发生错误会把errno这个全局的错误变量设置为本次执行库函数产生的错误码,errno是C语言提供的一个全局变量,可以直接使用,放在errno.h文件中的
#include<stdio.h> #include <errno.h> #include<string.h> int main() { //打开文件 FILE* pf = fopen("test.txt", "r"); if (NULL == pf) { //出错误的原因是什么 printf("%s\n", strerror(errno)); //No such file or directory return 0; } //读文件 //... //关闭文件 fclose(pf); pf = NULL; 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 | 任何可打印字符,包括图形字符和空白字符 |
例如:isdigit
char ch = '0'; if (ch >= '0' && ch <= '9') { //复杂 } if (isdigit(ch)) { //方便快捷 }
int tolower ( int c ); //把大写转为小写 int toupper ( int c ); //把小写转为大写
#include<stdio.h> #include <ctype.h> int main() { char ch = 0; while (ch = getchar()) { if (islower(ch)) { ch = toupper(ch); } else { ch = tolower(ch); } printf("%c", ch); } return 0; }
功能:可拷贝不同类型的数据
#include<stdio.h> #include<string.h> int main() { int arr1[] = { 1,2,3,4,5,6,7,8,9,10 }; int arr2[5] = { 0 }; memcpy(arr2, arr1, 5 * sizeof(arr1[0])); int i = 0; for (i = 0; i < 5; i++) { printf("%d ", arr2[i]); // 1 2 3 4 5 } return 0; }
注意:
void * memcpy ( void * destination, const void * source, size_t num );
#include<stdio.h> #include <assert.h> void* my_memcpy(void* dest, const void*src, size_t num) { void* ret = dest; assert(dest && src); while (num--) { *(char*)dest = *(char*)src; dest = (char*)dest + 1; src = (char*)src + 1; } return ret; } int main() { int arr3[] = { 1,2,3,4,5,6,7,8,9,10 }; int arr4[5] = { 0 }; my_memcpy(arr4, arr3+5, 5*sizeof(arr3[0])); int i = 0; for (i = 0; i < 5; i++) { printf("%d ", arr4[i]); //6 7 8 9 10 } return 0; }
其实,C语言只要求:
memcpy能拷贝不重叠的内存空间就可以了
memmove去处理那些重叠拷贝
功能:同样可拷贝不同类型的数据,不过可以重叠
#include<stdio.h> #include<string.h> int main() { int arr[] = { 1,2,3,4,5,6,7,8,9,10 }; memmove(arr + 2, arr, 20); int i = 0; for (i = 0; i < 10; i++) { printf("%d ", arr[i]); //1 2 1 2 3 4 5 8 9 10 } return 0; }
注意:
#include<stdio.h> #include <assert.h> void* my_memmove(void* dest, const void* src, size_t num) { void* ret = dest; assert(dest && src); 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; } 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 < 10; i++) { printf("%d ", arr[i]); //1 2 1 2 3 4 5 8 9 10 } return 0; }
功能:把一块内存空间设置成你想要的值,以字节为单位来修改
#include<stdio.h> #include<string.h> int main() { //char arr[20] = { 0 }; //memset(arr, 'x', 10); //printf("%s\n", arr); //xxxxxxxxxx int arr[10] = { 1,2,3,4,5,6,7,8,9,10 }; memset(arr, '\0', 10); int i = 0; for (i = 0; i < 10; i++) { printf("%d ", arr[i]); // 0 0 0 4 5 6 7 8 9 10 } //01 00 00 00 02 00 00 00 03 00 00 00 04 00 00 00 ...将前10个字节改为0 //00 00 00 00 00 00 00 00 00 00 00 00 04 00 00 00 return 0; }
功能:内存比较
int memcmp ( const void * ptr1, const void * ptr2, size_t num );
比较从ptr1和ptr2指针开始的num个字节,不在乎有无 '\0' ,你让它比较几个字节就比较几个字节。
#include<stdio.h> #include<string.h> int main() { int arr1[] = { 1,2,7,4,5 }; int arr2[] = { 1,2,3,4,5 }; printf("%d\n", memcmp(arr1, arr2, 9)); //1 // 9表示比较前9个字节 return 0; }