目录
1.求字符串长度
2.长度不受限制的字符串函数
3.长度受限制的字符串函数
4.字符串查找
5.错误信息报告
6.内存操作函数
1.求字符串长度
1.1 strlen
size_t strlen ( const char * str );
注意事项:
define _CRT_SECURE_NO_WARNINGS
include<stdio.h>
include<string.h>
int main()
{
char arr1[] = { 'b','i','t' };
int len = strlen(arr1);
printf("%d\n", len);
return 0;
}
define _CRT_SECURE_NO_WARNINGS
include<stdio.h>
include<string.h>
int main()
{
if (strlen("abc") - strlen("abcdef") > 0)
printf(">");
if (strlen("abc") - strlen("abcdef") < 0)
printf("<");
if (strlen("abc") - strlen("abcdef") == 0)
printf("==");
return 0;
}
1.2 strlen的模拟实现
define _CRT_SECURE_NO_WARNINGS
include<stdio.h>
include<assert.h>
//创建计数器方式
size_t my_strlen(const char* str)
{
size_t count = 0;
assert(str);
while (*str !='\0')
{
count++;
str++;
}
return count;
}
int main()
{
char arr[] = "abcdef";
size_t n = my_strlen(arr);
printf("%u\n", n);
return 0;
}
//指针-指针的方式
int my_strlen(char*s)
{
char*p=s;
while(*p!='\0')
p++;
return p -s;
}
//不能创建临时变量计数器
int my_strlen(const char*str)
{
if(*str=='\0')
return 0;
else return 1+my_strlen(str+1);
}
2.长度不受限制的字符串函数
2.1 strcpy
char strcpy(char destination, const char*source );
2.2 strcpy的模拟实现
define _CRT_SECURE_NO_WARNINGS
include<stdio.h>
include<assert.h>
char my_strcpy(char str1,const char* str2)
{
assert(str1);
assert(str2);
char*ret = str1;
while (*str1++ = *str2++)
{
;
}
return ret;
}
int main()
{
char arr1[] = "abcdef";
char arr2[20] = { 0 };
my_strcpy(arr2, arr1);
printf("%s\n",arr2);
return 0;
}
2.3 strcat
char strcat ( char destination, const char* source );
define _CRT_SECURE_NO_WARNINGS
include<stdio.h>
include<string.h>
int main()
{
char arr1[20] = "hello";
char arr2[] = "world";
strcat(arr1, arr2);
printf("%s\n", arr1);
return 0;
}
2.4 strcat 的模拟实现
define _CRT_SECURE_NO_WARNINGS
include<stdio.h>
include<assert.h>
char my_strcat(char dest, const char* src)
{
char* ret = dest;
assert(dest && src);
while(*dest != '\0')
{
dest++;
}
while (*dest++ = *src++)
{
;
}
return ret;
}
int main()
{
char arr1[20] = "hollo";
my_strcat(arr1, "world");
printf("%s\n", arr1);
return 0;
}
2.5 strcmp
int strcmp ( const char str1, const char str2 );
比较两个字符串
2.6 strcmp的模拟实现
define _CRT_SECURE_NO_WARNINGS
include<stdio.h>
int my_strcmp(const char str1, const char str2)
{
while (*str1 == *str2)
{
if (*str1 == '\0')
return 0;
str1++;
str2++;
}
return (*str1 - *str2);
}
int main()
{
char arr1[] = "abc";
char arr2[] = "abc";
int ret = my_strcmp(arr1, arr2);
if (ret == 0)
printf("==\n");
if (ret > 0)
printf(">\n");
if (ret < 0)
printf("<\n");
return 0;
}
3.长度受限制的字符串函数介绍
3.1 strncpy
char strncpy ( char destination, const char * source, size_t num )
define _CRT_SECURE_NO_WARNINGS
include<stdio.h>
include<string.h>
int main()
{
char arr1[10] = "abcde";
char arr2[] = "fghi";
strncpy(arr1, arr2, 7);
printf("%s\n", arr1);
return 0;
}
3.2 strncpy的模拟实现
char * __cdecl strncpy (
char * dest,
const char * source,
size_t count
)
{
char *start = dest;
while (count && (*dest++ = *source++) != '\0') /* copy string */
count--;
if (count) /* pad out with zeroes */
while (--count)
*dest++ = '\0';
return(start);
}
3.3 strncat
char strncat ( char destination, const char * source, size_t num );
define _CRT_SECURE_NO_WARNINGS
include<stdio.h>
include<string.h>
int main()
{
char arr1[10] = "abcde";
char arr2[] = "fghi";
strncat(arr1, arr2, 3);
printf("%s\n", arr1);
return 0;
}
3.4 strncat的模拟实现
char * __cdecl strncat (
char * front,
const char * back,
size_t count
)
{
char *start = front;
while (*front++)
;
front--;
while (count--)
if ((*front++ = *back++) == 0)
return(start);
*front = '\0';
return(start);
}
3.5 strncmp
int strncmp ( const char str1, const char str2, size_t num );
define _CRT_SECURE_NO_WARNINGS
include<stdio.h>
include<string.h>
int main()
{
char arr1[] = "abcde";
char arr2[] = "fghi";
int ret = strncmp(arr1, arr2, 3);
printf("%d\n", ret);
return 0;
}
4.字符串查找
4.1 strstr
char strstr ( const char str1, const char* str2);
define _CRT_SECURE_NO_WARNINGS
include<stdio.h>
include<string.h>
int main()
{
char arr1[] = "chencli";
char arr2[] = "enc";
char* ret = strstr(arr1, arr2, 3);
if (ret == NULL)
printf("字串不存在");
else
printf("找到字串为:%s\n", ret);
return 0;
}
4.2 strstr的模拟实现
define _CRT_SECURE_NO_WARNINGS
include<stdio.h>
include<string.h>
char my_strstr(const char str1, const char* str2)
{
assert(str1 && str2);
const char* s1 = str1;
const char* s2 = str2;
const char* p = str1;
while (*p)
{
s1 = p;
s2 = str2;
while (*s1 != '\0' && *s2 != '\0' && *s1 == *s2)
{
s1++;
s2++;
}
if (*s2 == '\0')
return p;
p++;
}
return NULL;
}
4.3 strtok
char strtok ( char str, const char* sep );
define _CRT_SECURE_NO_WARNINGS
include<stdio.h>
include<string.h>
int main()
{
const char* sep = "@.";
char email[] = "1234567@qq.com";
char cp[30] = { 0 };
strcpy(cp, email);//strtok函数找到str中的下一个标记,并将其用\0结尾,返回一个指向这个标记的指针
//(注:strtok函数会改变被操作的字符串,所以在使用strtok函数切分的字符串一般都是临时拷贝的内容并且可修改。)
char* ret = strtok(cp, sep);//strtok函数的第一个参数不为NULL,函数将找到str中第一个标记,strtok函数将保存它在字符串中的位置
printf("%s\n", ret);
ret = strtok(NULL, sep);//strtok函数的第一个参数为NULL,函数将在同一个字符串中被保存的位置开始,查找下一个标记
printf("%s\n", ret);
ret = strtok(NULL, sep);
printf("%s\n", ret);//如果字符串中不存在更多的标记,则返回NULL指针
return 0;
}
5.错误信息报告
5.1strerror
char* strerror ( int errnum );
define _CRT_SECURE_NO_WARNINGS
include<stdio.h>
include<string.h>
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));
return 0;
}
define _CRT_SECURE_NO_WARNINGS
include<stdio.h>
include<string.h>
int main()
{
FILE* pf = fopen("test.text", "r");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return 1;
}
else
{
//
}
return 0;
}
6.内存操作函数
6.1 memcpy
void memcpy ( void destination, const void* source, size_t num );
6.2 memcpy的模拟实现
define _CRT_SECURE_NO_WARNINGS
include<stdio.h>
include<assert.h>
void my_memcpy(void dest,const void* src, size_t num)
{
assert(dest && src);
void* ret = dest;
while (num--)
{
*(char*)dest = *(char*)src;
dest = (char*)dest + 1;
src = (char*)src + 1;
}
return ret;
}
int main()
{
char arr1[100] = "abcdef";
char arr2[100] = { 0 };
my_memcpy(arr2, arr1,5);
printf("%s\n", arr2);
return 0;
}
6.3 memmove
void memmove ( void destination, const void* source, size_t num );
6.4 memmove的模拟实现
void my_memmove(void dest, const void* src, size_t num)
{
assert(dest && src);
void* ret = dest;
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);
}
}
}
6.5 memset
填充内存块
define _CRT_SECURE_NO_WARNINGS
include<stdio.h>
include<string.h>
int main()
{
char arr[] = "hello world";
memset(arr + 6, 'x', 3);
printf("%s\n", arr);
return 0;
}