目录
1.strlen
2.strcpy
3.strcat
4.strcmp
5.strncpy
6.strncat
7.strncmp
8.strstr
9.strtok
10.strerror
11.memcpy
12.memmove
13.memcmp
1.strlen
该函数是求有效字符串的长度(不包括'\0'),其返回值是无符号整数
typedef unsigned int size_t;
size_t strlen(const char* str);
#include<stdio.h>
int main()
{
const char* str1 = "abcdef";
const char* str2 = "nbb";
//if(strlen(str2)>strlen(str1)) 比较字符串的大小
if (strlen(str2) - strlen(str1) > 0)//发生隐式转化
//无符号整数 -3会被转化成为巨大的无符号整数
{
printf("str2>str1\n");//√
}
else
{
printf("str2<str1\n");
}
return 0;
}
2.strcpy
该函数是进行字符串的拷贝,开辟空间时需要容纳'\0',使用所有的字符串函数都需要引入#include<string.h>
char* strcpy(char* destinztion,const char* cource);
#include<stdio.h>
#include<string.h>
int main()
{
const char* src = "abcd";
char dst[8];
strcpy(dst, src);//把src拷贝给dst
printf("%s\n",dst);
return 0;
}
3.strcat
该函数是进行字符串的拼接(追加拷贝)
char* strcat(char* destinztion,const char* cource);
#include<stdio.h>
#include<string.h>
int main()
{
const char* src = "abcd";
char dst[16] = "feg";
strcat(dst, src);//在dst后面拼接src
printf("%s\n", dst);
return 0;
}
注:(1)拼接也是拷贝
(2)需要dst预留足够大的空间,可以容纳拷贝内容
4.strcmp
该函数是进行字符串比较
int strcmp(const char* str1,const char* str2);
#include<stdio.h>
#include<string.h>
int main()
{
const char* str1 = "abcd75";
const char *str2 = "abcd";
int ret = strcmp(str1, str2);// str1 > str2, ret > 0; str1 < str2, ret < 0;
//str1 = str2, ret = 0;
printf("%d\n", ret);
return 0;
}
比较规则:从左向右,只要特定的字符不相等,哪个字符大,所在的字符串就大,长短无关,用阿斯克码值比较的
5.strncpy
该函数是进行指定个数的拷贝,默认不带'\0',是安全的接口
字符串的“安全”,不是不崩溃,也不是不报错,是接口是安全的,是想表明如果报错了,和我接口没有关系(总之,接口无责,影响可控)
char* strncpy(char* destination,const char* source,size_t num);
#include<stdio.h>
#include<string.h>
int main()
{
const char* src = "abcd";
char dst[8];
strncpy(dst, src, strlen(src) + 1);
printf("%s\n",dst);
return 0;
}
6.strncat
该函数是实现指定个数的拼接(自己给自己拼接是不可以的)
char* strncat(char* destination,const char* source,size_t num);
#include<stdio.h>
#include<string.h>
int main()
{
const char* src = "abcd";
char dst[16] = "feg";
strncat(dst, src,3);//拼接完整的将3改为strlen(src)+1
printf("%s\n", dst);
return 0;
}
7.strncmp
该函数是进行指定字符数的比较
int strncmp(const char* str1,const char* str2,size_t num)
#include<stdio.h>
#include<string.h>
int main()
{
const char* str1 = "abcd75";
const char* str2 = "abcd";
int ret = strncmp(str1, str2,6);
printf("%d\n", ret);
return 0;
}
puts():往显示器上打印字符串,不作处理
8.strstr
该函数是进行子串查找
char* strstr(const char* str1,const char* str2);
#include<stdio.h>
#include<string.h>
int main()
{
//char* strstr(const char* target,const char* sub_str);
const char* target = "abc123defi123xyz";
const char* sub_str = "123";
char* found = strstr(target, sub_str);
found++;
//printf("%s\n", strstr(found, sub_str));//在target中找sub_str第二次出现
printf("%s\n", found);//在target中找sub_str第一次出现
}
9.strtok
该函数是字符串的切分,用分隔符进行切分,需要多次调用,才能彻底完成切分(返回值!=NULL,NULL就结束截取)
C中一般只有一个返回值
char* strtok(char* str,const char*sep)
// 切分的字符串 分隔符(可以指定多个)
#include<stdio.h>
#include<string.h>
int main()
{
char str[] = "hello,world|hello world";
char* sub1 = strtok(str, ",|");
printf("%s\n", sub1);//hello
//如果截取剩下的字符串需要第二次之后,调用的时候,str -> NULL
char* sub2 = strtok(NULL, ",|");
printf("%s\n", sub2);//hello
//world
//第二次之后,函数是由于strtok中定义有类似于static类型的变量,故能记得老的字符串
}
原理:(1)strtok一定有类似于全局或者static类型的char*变量,保存历史剩余字符串,用来进行后续截取
(2)通过设置目标串中的分隔符->\0 的方式,进行子串切割
#include<stdio.h>
#include<string.h>
int main()
{
char str[] = "hello,world|hello world";
char* sub = strtok(str, ",|");
while (sub != NULL)
{
printf("sub_string:%s\n", sub);
sub = strtok(NULL, ",| ");
}
}
补充:(1)有连续的分隔符(全部忽略)
(2)切了一部分,想切新的字符串直接传入就好了
(3)要分割的字符串,必须是可写的(可修改的)
10.strerror
该函数是错误控制的函数
与库函数相关的,C中,有一个全局变量,errno,用来记录库函数调用的时候,出错了,是因为什么原因出错
将错误码->错误码描述
char* strerror(int errnum);
#include<stdio.h>
#include<string.h>
int main()
{
for (int i = 0; i < 50; i++)
{
printf("%d:%s\n", i, strerror(i));
}
}
字符分类函数:
#include<stdio.h>
#include<string.h>
#include<ctype.h>//字符分类函数需要引入
int main()
{
char a = ' ';
if (isspace(a))//判断:空格' '; 换页'\f'; 换行'\n'; 回车'\r';
{
printf("yes!\n");
}
char b = '1';
if (isdigit(b))//判断十进制数字 '0'-> '9'
{
printf("yes!\n");
}
char c = 'a';
printf("%c\n", toupper(c));//小写转大写
char d = 'A';
printf("%c\n", tolower(d));//大写转小写
return 0;
}
#include<stdio.h>
#include<string.h>
#include<ctype.h>//字符分类函数需要引入
int main()
{
char* str = "aAbBcCdD";
char big[10];
char small[10];
char* start = str;
int i = 0;
int j = 0;
while (*start != '\0')
{
if (islower(*start))
{
small[i++] = *start;//将小写字母保存到small[]
}
else if (isupper(*start))
{
big[j++] = *start;
printf("%c ", big[i]);//将大写字母保存到big[]
}
else
{
}
start++;
}
return 0;
}
11.memcpy
该函数是内存操作函数(以mem开头的函数)(凌驾于一切操作函数之上),没有类型,只有内存空间
#include<stdio.h>
#include<string.h>
int main()
{
const char* src = "hello world";
char dst[16];
memcpy(dst, src, strlen(src) + 1);//完成字符串拷贝
printf("%s\n", dst);
}
#include<stdio.h>
#include<string.h>
struct stu
{
char name[16];
int age;
};
int main()
{
struct stu zhangsan = { "张三",18 };
struct stu lisi;
memcpy(&lisi, &zhangsan, sizeof(zhangsan));//结构体的拷贝
printf("%s %d\n", lisi.name,lisi.age);
}
12.memmove
该函数约等于memcpy,也是内存操作函数
memcpy函数和memmove的区别:可能有老的版本的书和编译器会有内存重叠的问题,memmove能处理,而memcpy处理不了,但是新的编译器已经完全能处理了
13.memcmp
该函数是比较字符串、数组等等
#include<stdio.h>
#include<string.h>
int main()
{
char buffer1[] = "DWgaOtP12df0";
char buffer2[] = "DWGAOTP12DF0";
int n;
n = memcmp(buffer1, buffer2, sizeof(buffer1));
if (n > 0)
{
printf("'%s' is greater than '%s'.\n", buffer1, buffer2);//√
}
else if (n < 0)
{
printf("'%s' is less than '%s'.\n", buffer1, buffer2);
}
else
{
printf("'%s' is the same as '%s'.\n", buffer1, buffer2);
}
return 0;
}