赞
踩
size_t strlen(const char* str);
'\0'
为结束标志,strlen函数返回的是在字符串中 ‘\0’ 前面出现的字符个数(不包含’\0’ )。#include<stdio.h> #include<string.h> int main() { const char* str1 = "abcdef"; const char* str2 = "bbb"; if (strlen(str2) - strlen(str1) > 0) { printf("str2>str1\n"); } else { printf("str1>str2\n"); } return 0; } //看起来打印结果是str1>str2,但实际上是str2>str1 //因为strlen是无符号size_t整型,|3-6|=3>0
#include<stdio.h> #include<string.h> #include<assert.h> size_t my_strlen(const char* str) { assert(str);//防止空指针 const char* start = str; const char* end = str; while (*end != '\0') { end++; } return end - start; } //指针-指针方式 size_t my_strlen(const char* s) { char* p = s; while (*p != '\0') p++; return p - s; } //计数器方式 int 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; }
char* strcpy(char * destination, const char * source );
#include<stdio.h>
#include<string.h>
int main()
{
char arr1[10] = "xxxxxxxxx";
char arr2[] = { 'b', 'i', 't', '\0' };
strcpy(arr1, arr2);
printf("%s\n", arr1);//bit
return 0;
}
//结果:bit
//注:因为arr1数组的容量比arr2大,因此如果strcpy(arr2,arr1)的话
//会造成越界访问 导致代码出现错误
#include<stdio.h> #include<string.h> #include<assert.h> char* my_strcpy(char* dest, const char* src) { assert(dest && src); char* ret = dest; while ((*dest++ = *src++)) { ; } return ret; } int main() { char arr1[10] = "abcdef"; char arr2[] = "bit"; my_strcpy(arr1, arr2); printf("%s", arr1); return 0; } //结果:bit
char* strcat(char* destination, const char* source);
语法原理:
特点:
#include<stdio.h>
#include<string.h>
int main()
{
char arr1[20] = "你好";
char arr2[] = "hello world";
strcat(arr1, arr2);
printf("%s\n", arr1);
return 0;
}
//结果:你好hello world
#include<stdio.h> #include<string.h> #include<assert.h> char* my_strcat(char* dest, const char* src) { assert(dest); assert(src); //1.先找目标空间中的\0 char* cur = dest; while (*cur != '\0') { cur++; } //2.拷贝源头数据到\0之后的空间 while (*cur++ = *src++) { ; } return dest;//返回目标空间起始地址 } int main() { char arr1[20] = "hello ";//[]里如果不加数会导致数组越界访问 char arr2[] = "world"; printf("%s\n", my_strcat(arr1, arr2)); return 0; }
int strcmp(const char* str1, const char* str2);
对应比较
,哪一个字符的ASCII码大,哪一个 字符串大,如果两个字符一样,就继续比较下一个字符。大于0
的数字0
小于0
的数字#include<stdio.h> #include<string.h> int main() { //char arr1[] = "abcdef"; //char arr2[] = "abq";//返回了一个小于0的数字(-1) char arr1[] = "abcd"; char arr2[] = "abc";//返回了一个大于0的数字(1) //char arr1[] = "abc"; //char arr2[] = "abc";//返回0 // //char arr1[] = { 'a', 'b', 'c' };//err // //char arr2[] = { 'a', 'b', 'c' };//err int ret = strcmp(arr1, arr2); if (ret < 0) printf("arr1<arr2\n"); else if (ret > 0) printf("arr1>arr2\n"); else printf("arr1==arr2\n"); printf("%d\n", ret); return 0; }
#include<stdio.h> #include<string.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++; } //方法一: /*if (*str1 > *str2) { return 1; } else return -1;*/ //方法二: return *str1 - *str2; } int main() { char arr1[] = "abcd"; char arr2[] = "abc"; int ret = my_strcmp(arr1, arr2); printf("%d\n", ret); return 0; }
char* strncpy(char* destination, const char* source, size_t num);
#include<stdio.h>
#include<string.h>
int main()
{
char arr1[10] = "abcedf";
char arr2[10] = "xxxxxxxx";
strncpy(arr2, arr1, 8);
//8位看上去多了,但实际多了的位数后面自动添加'\0',直到补齐8位为止,
//剩余还有2个空间是本身自带的'\0'
printf("%s\n", arr2);
return 0;
}
//结果:abcedf('\0''\0''\0''\0')
#include<stdio.h> #include<string.h> #include<assert.h> char* my_strncpy(char* dest, const char* src, size_t num) { assert(dest && src); char* ret = dest; while (num-- && *src != '\0') { *dest++ = *src++; } return ret; } int main() { char arr1[] = "abcdef"; char arr2[] = "xxxxxxxx"; int ret = my_strncpy(arr2, arr1, 5); printf("%s\n", arr2); return 0; }
char* strncat(char* destination, const char* source, size_t num);
#include<stdio.h>
#include<string.h>
int main()
{
char arr1[20] = "abcdef\0qqqq";
char arr2[] = "xyz";
strncat(arr1, arr2, 2);
printf("%s\n", arr1);
}
//结果:abcdefxy('\0')(自己添加的'\0')
#include<stdio.h> #include<assert.h> char* my_strncat(char* dest, const char* src, size_t num) { assert(dest && src); char* ret = dest; while (*dest != '\0') { dest++; } while (num-- && *src != '\0') { *dest++ = *src++; } return ret; } int main() { char arr1[20] = "abc"; char arr2[] = "xyz"; printf("%s\n", my_strncat(arr1, arr2, 2)); return 0; }
int strncmp(const char* str1, const char* str2, size_t num)
#include <stdio.h> #include <string.h> int main() { char arr1[12] = "abcdef"; char arr2[20] = "abcfg"; int ret = strncmp(arr1, arr2, 3); printf("%d\n", ret); if (ret > 0) { printf("arr1>arr2"); } else if (ret == 0) { printf("arr1=arr2"); } else { printf("arr1<arr2"); } return 0; } //结果:0 arr1=arr2
#include<stdio.h> #include<assert.h> int my_strncmp(const char* str1, const char* str2, size_t num) { assert(str1 && str2); while (num-- && *str1 == *str2 && *str1 != '\0') { if (num > 1) { str1++; str2++; } } return *str1 - *str2; } int main() { char arr1[12] = "abcdef"; char arr2[20] = "abcfg"; int ret = my_strncmp(arr1, arr2, 3); if (ret > 0) { printf("arr1>arr2"); } else if (ret == 0) { printf("arr1=arr2"); } else { printf("arr1<arr2"); } return 0; }
char* strstr(const char* str1, const char* str2);
#include<stdio.h>
#include<string.h>
int main()
{
char arr1[] = "abcdef";
char arr2[] = "bcd";
char* p = strstr(arr1, arr2);
if (p == NULL)
printf("不存在\n");
else
printf("%s\n", p);
}
//结果:bcdef('\0')
#include<stdio.h> #include<assert.h> char* my_strstr(const char* str1, const char* str2) { assert(str1 && str2); char* s1 = (char*)str1; char* s2 = (char*)str2; char* cp = (char*)str1;//cp保存首字符的地址 while (*cp) { s1 = cp; while (*s1 != '\0' && *s2 != '\0' && *s1 == *s2) { s1++; s2++; } if (*s2 == '\0') { return cp; } s2 = (char*)str2; cp++;//cp++可以得到原起始位置的下一个位置 } return NULL; } int main() { char arr1[] = "abcdef"; char arr2[] = "bcd"; char* p = my_strstr(arr1, arr2); if (p == NULL) printf("不存在\n"); else printf("%s\n", p); }
char* strtok(char* str, const char* sep);
待拆分字符串
所有的分割符号
NULL
即可继续分割(NULL的时候才会用上记忆功能)第一个q
),内部存储的指针指向下一个\0#include<stdio.h> #include<string.h> int main() { char arr1[] = "10535715@qq.com"; const char* p = "@."; //方法一:方便理解 //首元素地址非空 //char* str = strtok(arr1, p); //printf("%s\n", str);//10535715 //第一个\0开始算起,strtok函数的第一个参数为NULL,直到遇到.将其变成\0 //str = strtok(NULL, p); //printf("%s\n", str);//qq //第二个\0开始算起,第一个参数还是NULL //str = strtok(NULL, p);//com //printf("%s\n", str); //方法二: char* str = NULL; for (str = strtok(arr1, p); str != NULL; str = strtok(NULL, p)) { printf("%s\n", str); } return 0; }
char * strerror ( int errnum );
#include<stdio.h> #include<string.h> #include<errno.h> int main() { printf("%s\n", strerror(0));//0,1,2,3,4分别为错误码(c语言中库函数报错的时候的错误码) printf("%s\n", strerror(1)); printf("%s\n", strerror(2)); printf("%s\n", strerror(3)); printf("%s\n", strerror(4)); return 0; } //结果: //No error //Operation not permitted //No such file or directory //No such process //Interrupted function call //如果错误信息是字符串的话,返回的是每行首字符的地址
#include<stdio.h> #include<string.h> #include<errno.h> int main() { FILE* pf = fopen("test.txt", "r"); if (pf == NULL) { perror("");//打印的依然是errno变量中错误码对应的错误信息 //perror=printf+strerror //printf("%s\n", strerror(errno)); return 1; } //读文件 fclose(pf); pf = NULL; return 0; } //No such file or directory
可以判断字符是否为所需要的那一种类型的字符
下面是这部分的一些函数:(头文件为ctype.h)
void * memcpy ( void * destination, const void * source, size_t num );
语法原理:
特点:
不能重叠拷贝
#include<stdio.h>
#include<string.h>
int main()
{
int arr1[10] = { 1,2,3,4,5,6,7,8,9,10 };
int arr2[10] = { 0 };
memcpy(arr2, arr1, 8);//把arr1中的前8个字节的数拷贝到arr2里面去
int i = 0;
for (i = 0; i < 10; i++)
{
printf("%d ", arr2[i]);
}
return 0;
}
//结果:1 2 0 0 0 0 0 0 0 0
memcpy与strncpy很类似,但memcpy没规定参数
#include<stdio.h> #include <assert.h> #include<string.h> void* my_memcpy(void* dest, void* src, size_t num) { void* ret = dest;//定义起始位置ret assert(dest); assert(src); while(num--) { *(char*)dest = *(char*)src;//void*指针无法解引用 dest = (char*)dest + 1;//有些编译器不能dest++和src++ src = (char*)src + 1; } return ret; } int main() { int arr1[] = { 1,2,3,4,5,6,7,8,9,10 }; int arr2[10] = { 0 }; my_memcpy(arr2, arr1, 20);//20为数组arr1中5个int元素的大小 int i = 0; for (i = 0; i < 10; i++) { printf("%d ", arr2[i]);//1 2 3 4 5 0 0 0 0 0 } return 0; }
void * memmove ( void * destination, const void * source, size_t num );
#include<stdio.h>
#include<string.h>
int main()
{
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
memmove(arr + 2, arr, 20);//在arr数组的首元素+2的位置处拷贝arr数组中前5个元素
int i = 0;
for (i = 0; i < 10; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
//结果:1 2 1 2 3 4 5 8 9 10
#include<stdio.h> #include<string.h> #include<assert.h> void* my_memmove(void* dest, const void* src, size_t num) { char* s1 = (char*)dest; char* s2 = (char*)src; if (s1 <= s2) { while (num--)//dest小于src时,从前向后拷贝 { *s1 = *s2; s1++; s2++; } } else { while (num--) { *(s1 + num) = *(s2 + num);//dest大于src时,从后向前拷贝 } } return dest; } int main() { int arr[10] = { 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; }
void * memset ( void * ptr, int value, size_t num );
#include<stdio.h>
#include<string.h>
int main()
{
int arr[] = { 1,2,3,4,5 };
memset(arr, 0, 8);//用0代替数组arr中的前2个元素(8个字节)
int i = 0;
for (i = 0; i < 5; i++)
{
printf("%d ", arr[i]);//0 0 3 4 5
}
return 0;
}
#include<stdio.h> #include<assert.h> void* my_memset(void* ptr, int value, size_t num) { void* ret = ptr; assert(ptr); while (num--) { *(char*)ptr = value; ptr = (char*)ptr + 1; } return ret; } int main() { int arr[] = { 1,2,3,4,5,6,7,8,9,10 }; my_memset(arr, 0, 4); int i = 0; for (i = 0; i < 10; i++) { printf("%d ", arr[i]);//0 2 3 4 5 6 7 8 9 10 } return 0; }
int memcmp ( const void * ptr1, const void * ptr2, size_t num );
#include<stdio.h> #include<string.h> int main() { int arr1[] = { 1,2,3,4 };//小端存储:01 00 00 00 02 00 00 00 03 00 00 00 04 00 00 00 int arr2[] = { 1,2,3,0 };// 01 00 00 00 02 00 00 00 03 00 00 00 00 00 00 00 int ret = memcmp(arr1, arr2, 12);//按照内存在第十二个字节数进行比较->00比00->返回0 printf("%d\n", ret); return 0; }#include<stdio.h> #include<assert.h> int my_memcmp(const void* str1, const void* str2, size_t count) { assert(str1); assert(str2); if (!count) { return 0; } while (--count && *(char*)str1 == *(char*)str2) { str1 = (char*)str1 + 1; str2 = (char*)str2 + 1; } return ((unsigned char*)str1 - (unsigned char*)str2); } int main() { int arr1[] = { 1,2,3,4,5 }; int arr2[] = { 0 }; int ret = my_memcmp(arr2, arr1, 12);//返回了一个大于0的数 printf("%d\n", ret); return 0; }
memcmp和strncmp很类似,但memcmp没规定参数。
#include<stdio.h> #include<assert.h> int my_memcmp(const void* str1, const void* str2, size_t count) { assert(str1); assert(str2); if (!count) { return 0; } while (--count && *(char*)str1 == *(char*)str2) { str1 = (char*)str1 + 1; str2 = (char*)str2 + 1; } return ((unsigned char*)str1 - (unsigned char*)str2); } int main() { int arr1[] = { 1,2,3,4,5 }; int arr2[] = { 0 }; int ret = my_memcmp(arr2, arr1, 12);//返回了一个大于0的数 printf("%d\n", ret); return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。