BIT-3-字符函数和字符串函数
1. 函数介绍
1.1 strlen
size_t strlen ( const char * str );
-
字符串已经 '\0' 作为结束标志,strlen函数返回的是在字符串中 '\0' 前面出现的字符个数(不包
-
含 '\0' )。
-
参数指向的字符串必须要以 '\0' 结束。
-
注意函数的返回值为size_t,是无符号的( 易错 )
-
学会strlen函数的模拟实现
#include <stdio.h> #include <string.h> int main() { //char ch = 'a'; //"abcdef";//c语言没有字符串类型 //char arr[]="abcdef"; char arr[] = { 'a','b','c','d','e','f' }; int len = strlen(arr);//string length printf("%d\n", len); return 0; }
因为 strlen
返回的是无符号数,无符号数-无符号数一定是>
#include <stdio.h> #include <string.h> #include <assert.h> //int my_strlen(const char* str) 1.计数器的方法 size_t my_strlen(const char* str) { int count = 0; assert(str != NULL); while (*str != '\0') { count++; str++; } return count; } //2.递归的方法 //3.指针-指针的方法 int main() { char arr[] = "abcdef"; int len = my_strlen(arr); printf("%d\n", len); return 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 the terminating null character (and stopping at that point).
-
源字符串必须以 '\0' 结束。
-
会将源字符串中的 '\0' 拷贝到目标空间。
-
目标空间必须足够大,以确保能存放源字符串。
-
目标空间必须可变。
-
学会模拟实现。
int main() { char arr1[20] = { 0 }; char arr2[] = "abcdef"; strcpy(arr1, arr2); printf("%s\n", arr1); return 0; }
一定要保证在原字符串arr2中找到'\0'
,for则无法复制
//strcpy函数返回的是目标空间的起始地址 strcpy函数的返回类型是为了链式访问 char* my_strcpy(char* dest,const char* src) { assert(dest != NULL); assert(src != NULL); char* ret = dest; while (*src != '\0') { /**dest = *src; dest++; src++;*/ *dest++ = *src++; } *dest = *src;// \0 return ret; } int main() { char arr1[20] = { 0 }; char* arr2 = "hello bit"; my_strcpy(arr1, arr2); printf("%s\n", arr1); 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 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' 结束。
-
目标空间必须有足够的大,能容纳下源字符串的内容。
-
目标空间必须可修改。
-
字符串自己给自己追加,如何?
my_strcat(char* dest, const char* src) { assert(dest && src); //找目标空间中的\0 char* ret = dest; while (*dest) { dest++; } //拷贝 while (*dest++ = *src++) { ; } return ret; } int main() { char arr1[20] = "hello"; char arr2[] = "bit"; my_strcat(arr1, arr2); printf("%s\n",arr1); 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 each other, it continues with the following pairs until the characters differ or until a terminating null-character is reached.
-
标准规定:
第一个字符串大于第二个字符串,则返回大于0的数字
第一个字符串等于第二个字符串,则返回0
第一个字符串小于第二个字符串,则返回小于0的数字
strcmp函数比较的不是字符串的长度!!!而是比较字符串中对应位置上的字符的大小,如果相同,直到不同或者都遇到\0
int main() { char arr1[] = "abcdef"; char arr2[] = "abq"; int ret = strcmp(arr1, arr2); printf("%d\n", ret); return 0; } // 1 0 -1
int my_strcmp(const char* s1, const char* s2) { while (*s1 == *s2) { if (*s1 == '\0') { return 0; } s1++; s2++; } if (*s1 > *s2) return 1; else return -1; } int main() { char arr1[] = "abcdef"; char arr2[] = "abq"; int ret = my_strcmp(arr1, arr2); if (ret > 0) { printf(">\n"); } else if (ret == 0) { printf("=\n"); } else { printf("<\n"); } printf("%d\n", ret); return 0; }
长度不受限制的字符串函数:strcpy strcat strcmp
长度受限制的字符串函数:strncpy strncat strncmp
1.5 strncpy
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个字符从源字符串到目标空间。
-
如果源字符串的长度小于num,则拷贝完源字符串之后,在目标的后边追加0,直到num个。
1.6 strncat
char * strncat ( char * destination, const char * source, size_t num );
-
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
#include <stdio.h> #include <string.h> int main () { char str1[20]; char str2[20]; strcpy (str1,"To be "); strcpy (str2,"or not to be"); strncat (str1, str2, 6); puts (str1); return 0; }
1.7 strncmp
int strncmp ( const char * str1, const char * str2, size_t num );
比较到出现另个字符不一样或者一个字符串结束或者num个字符全部比较完。
1.8 strstr
char * strstr ( const char *str1, cons t char * str2);
看str2是不是str1的一个子串
int main() { char arr1[] = "abcdefgh"; char arr2[] = "abcd"; char* ret = strstr(arr1, arr2); if (ret == NULL) { printf("找不到子串"); } else { printf("%s\n", ret); } return 0; }
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 cur; } cur++; } return NULL;//找不到 } int main() { char arr1[] = "abcdefgh"; char arr2[] = "abca"; char* ret = my_strstr(arr1, arr2); if (ret == NULL) { printf("找不到子串"); } else { printf("%s\n", ret); } return 0; }
1.9 strtok
char * strtok ( char * str, const char * sep );
-
sep参数是个字符串,定义了用作分隔符的字符集合
-
第一个参数指定一个字符串,它包含了0个或者多个由sep字符串中一个或者多个分隔符分割的标记。
-
strtok函数找到str中的下一个标记,并将其用 \0 结尾,返回一个指向这个标记的指针。(注:strtok函数会改变被操作的字符串,所以在使用strtok函数切分的字符串一般都是临时拷贝的内容并且可修改。)
-
strtok函数的第一个参数不为 NULL ,函数将找到str中第一个标记,strtok函数将保存它在字符串中的位置。
-
strtok函数的第一个参数为 NULL ,函数将在同一个字符串中被保存的位置开始,查找下一个标记。
-
如果字符串中不存在更多的标记,则返回 NULL 指针。
int mian() { char arr[]="dsaffaeeaw@year.net"; char buf[20]={0}; strcpy(buf,arr); const char* sep = "@."; //stock //"@." char* str = NULL; for(str=strtok(buf,sep);str!=NULL;str=strtok(NULL,sep)) { printf("%s\n",str); } // strtok(buf,sep);//只找第一个标记 // strtok(NULL,sep);//是从保存好的位置开始继续往后找 return 0; }
1.10 strerror
char * strerror ( int errnum );
返回错误码,所对应的错误信息。
字符分类函数:
函数 | 如果他的参数符合下列条件就返回真 |
---|---|
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 | 任何可打印字符,包括图形字符和空白字符 |
字符转换函数:
int tolower ( int c );//大写转小写 int toupper ( int c );//小写转大写
1.11 memcpy
void * memcpy ( void * destination, const void * source, size_t num );
-
函数memcpy从source的位置开始向后复制num个字节的数据到destination的内存位置。
-
这个函数在遇到 '\0' 的时候并不会停下来。
-
如果source和destination有任何的重叠,复制的结果都是未定义的。
1.12 memmove
void * memmove ( void * destination, const void * source, size_t num );
-
和memcpy的差别就是memmove函数处理的源内存块和目标内存块是可以重叠的。
-
如果源空间和目标空间出现重叠,就得使用memmove函数处理。
1.13 memcmp
int memcmp ( const void * ptr1, const void * ptr2, size_t num );
比较从ptr1和ptr2指针开始的num个字节
BIT-4-自定义类型:结构体,枚举,联合
1 结构体的声明
1.1 结构的基础知识
结构是一些值的集合,这些值称为成员变量。结构的每个成员可以是不同类型的变量。
1.2 结构的声明
struct tag { member-list; }variable-list;
1.3 特殊的声明
在声明结构的时候,可以不完全的声明
//匿名结构体类型 struct { int a; char b; float c; }x; struct { int a; char b; float c; }a[20], *p;
1.4 结构的自引用
//代码1 struct Node { int data; struct Node next; }; //可行否? 不可 如果可以,那sizeof(struct Node)是多少?