1.strlen(求字符串长度)
定义:size_t strlen (const char* string), 其中size_t 是 unsigned int size_t
#include<stdio.h>
#include<string.h>
int main()
{
int len = strlen("abcdef");
printf("%d\n", len);
return 0;
}
2.strcpy(字符串拷贝)
定义:char*strcpy (char* destination , const char* source)
#include<stdio.h>
#include<string.h>
int main()
{
char arr1[] = "abcdefg";
char arr2[] = "bit";
strcpy(arr1, arr2);// 把\0 也拷贝过去了
printf("%s\n", arr1);
return 0;
}
3.strcat(字符串追加)
定义:char* strcat (char* destination , const char* source)
#include<stdio.h>
#include<string.h>
int main()
{
char arr1[30] = "abc";
char arr2[] = "def";
strcat(arr1, arr2);
printf("%s\n", arr1);
return 0;
}
4.strcmp(字符串比较)
定义:int strcmp (const char* str1 , const char* str2)
#include<stdio.h>
#include<string.h>
int main()
{
char* p1 = "abcdef";
char* p2 = "sqwer";
int ret = strcmp(p1, p2);//比较的是对应的字符
printf("%d\n", ret);//小于的话返回负数//大于的话返回正数//等于返回0
return 0;
}
5.strtok(打断函数)
定义:char* strtok (char*str, const char*sep)
#include<stdio.h>
#include<string.h>
#include<assert.h>
int main()
{
char arr[] = "192.168.31.121";
char* p = ".";
char* ret = NULL;
for (ret = strtok(arr, p);ret != NULL;ret = strtok(NULL, p))
{
printf("%s\n", ret);
}
return 0;
}
#include<stdio.h>
#include<string.h>
#include<assert.h>
int main()
{
char arr[] = "zpw@bitedu.tech";
char* p = "@.";
char* ret = NULL;
for (ret = strtok(arr, p);ret != NULL;ret = strtok(NULL, p))
{
printf("%s\n",ret);
}
return 0;
}
6.strerror(报错函数)
定义:char* strerror (int errnum)
#include<stdio.h>
#include<string.h>
#include<assert.h>
#include<errno.h>
int main()
{
/*char* str = strerror(4);
printf("%s\n", str);*/
char* str = strerror(errno);
//errno是一个全局的错误码变量,当C语言的库函数在执行过程中发生了错误,就会把对应的错误码,赋值到errno中
//头文件是<errno.h>
FILE* pf = fopen("1.txt", "r");//打开文件
if (pf == NULL)
{
printf("%s\n", strerror(errno));
}
else
{
printf("open file successed\n");
}
return 0;
}
7.islower(判断是否是小写字母)
#include<stdio.h>
#include<ctype.h>
int main()
{
char ch = 'w';
int ret = islower(ch);//非0就是小写字母//头文件是<ctype.h>
printf("%d\n", ret);
return 0;
}
8.isdigit(判断是否是数字)
#include<stdio.h>
int main()
{
char ch = '2';
int ret = isdigit(ch);//非0就是数字
printf("%d\n", ret);
return 0;
}
9.字符转换函数
#include<stdio.h>
int main()
{
char ch = tolower('Q');//转换成小写字母
putchar(ch);
char chh = toupper('m');//转换成大写字母
putchar(chh);
return 0;
}
#include<stdio.h>
#include<ctype.h>
int main()
{
char arr[] = "I AM A STUDENT";
int i = 0;
while (arr[i])
{
if (isupper(arr[i]))//判断是否是大写字母
{
arr[i] = tolower(arr[i]);//转换成小写字母
}
i++;
}
printf("%s\n", arr);
return 0;
}
10.memcpy(内存拷贝)
定义:void* memcpy (void* destination , const void* source , size_t num)
#include<stdio.h>
#include<string.h>
int main()
{
int arr1[] = { 1,2,3,4,5 };
int arr2[5] = { 0 };
memcpy(arr2, arr1, sizeof(arr1));//处理不重叠的//头文件是<string.h>
int i = 0;
for (i = 0;i < 5;i++)
{
printf(" %d", arr2[i]);
}
return 0;
}
11.memmove(内存拷贝)
定义:void* memmove(void* dest , const void*src , size_t count)
#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);//处理内存重叠的//头文件是<string.h>
int i = 0;
for (i = 0;i < 10;i++)
{
printf(" %d", arr[i]);
}
return 0;
}
12.memcmp(内存比较)
定义: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,5 };
int arr2[] = { 1,2,5,4,3 };
int ret = memcmp(arr1, arr2, 8);//8的单位是字节
printf("%d\n", ret);
return 0;
}
13.memset(内存设置)
定义:void* memset (void* dest , int c , size_t count)
#include<stdio.h>
#include<string.h>
int main()
{
char arr[10] = "";
memset(arr, '#', 10);//10表示10个字符
return 0;
}