当前位置: 首页 > news >正文

【C语言】字符函数和字符串函数

 

目录

1.求字符串长度strlen

2.长度不受限制的字符串函数

字符串拷贝strcpy

字符串追加strcat

字符串比较strcmp

3.长度受限制的字符串函数介绍strncpy

strncat

​编辑strncmp

4.字符串查找strstr

5.字符串分割strtok

6.错误信息报告

strerror

perror

7.字符分类函数

8.字符转换函数

 9.内存操作函数

memcpy

memmove

 memset

 memcmp​编辑


1.求字符串长度strlen

函数介绍

  • 字符串已经 '\0' 作为结束标志,strlen函数返回的是在字符串中 '\0' 前面出现的字符个数(不包含 '\0' )。
  • 参数指向的字符串必须要以 '\0' 结束。
  • 注意函数的返回值为size_t,是无符号的( 易错 ) 

使用示例

int main()
{size_t sz = strlen("abcd");printf("%u", sz);return 0;
}

因为函数的返回值为无符号类型,所以对于下面这种代码,我们很容易就看错:

#include<stdio.h>
#include<string.h>
int main()
{if (strlen("abc") - strlen("abcdef") > 0){printf("大于\n");}else{printf("小于\n");}return 0;
}

输出结果:

 正确的比较方法:

if (strlen("abc") > strlen("abcdef"))

strlen的模拟实现

size_t my_strlen(const char* str)
{int count = 0;while (*str!='\0'){count++;str++;}return count;
}


2.长度不受限制的字符串函数

字符串拷贝strcpy

  • 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[] = "hello world";strcpy(arr1, arr2);printf("%s", arr1);return 0;
}

输出结果: 

strcpy的模拟实现

char* my_strcpy(char* dest, const char* src)
{assert(dest && src);char* ret = dest;while (*dest++ = *src++){}return ret;
}

字符串追加strcat

  •  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' 结束。
  • 目标空间必须有足够的大,能容纳下源字符串的内容。
  • 目标空间必须可修改。
  • 字符串自己给自己追加,如何?

 使用示例

int main()
{char arr1[20] = "hello ";char arr2[] = "world";strcat(arr1, arr2);printf("%s", arr1);return 0;
}

输出结果: 

 strcat的模拟实现

char* my_strcat(char* dest, const char* src)
{assert(dest && src);char* ret = dest;while (*dest != '\0')//找到目标空间的\0{dest++;}while (*dest++ = *src++){}return ret;
}

注意:字符串不能自己给自己追加 

当字符串自己给自己追加时,字符串中的'\0'将会被覆盖,导致程序陷入死循环。

字符串比较strcmp

  •  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的数字

使用示例

int main()
{int ret1 = strcmp("abcdef", "abq");int ret2 = strcmp("abc", "abc");printf("%d\n", ret1);printf("%d\n", ret2);return 0;
}

 输出结果: 

strcmp的模拟实现

int my_strcmp(const char* str1, const char* str2)
{assert(str1 && str2);while (*str1++ == *str2++){if (*str1 == '\0'){return 0;}}return *str1 - *str2;
}


3.长度受限制的字符串函数介绍
strncpy

  • 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个。

使用示例

int main()
{char arr1[20] = "abcdef";char arr2[20] = "xxxxxxxxxxxxxx";strncpy(arr1, arr2, 3);printf("%s\n", arr1);return 0;
}

输出结果: 

strncat

  • 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 terminating null-character is copied.

使用示例

int main()
{char arr1[20] = "hello ";char arr2[20] = "worldxxx";strncat(arr1, arr2, 5);printf("%s\n", arr1);return 0;
}

输出结果: 


strncmp

 比较到出现另个字符不一样或者一个字符串结束或者num个字符全部比较完。

 使用示例

int main()
{char arr1[20] = "abc";char arr2[20] = "abcdef";char arr3[20] = "aba";printf("%d\n", strncmp(arr1, arr2, 3));printf("%d\n", strncmp(arr1, arr2, 4));printf("%d\n", strncmp(arr1, arr3, 5));return 0;
}

输出结果: 


4.字符串查找strstr

strstr函数是在字符串str1中查找是否含有字符串str2,如果存在,返回str2在str1中第一次出现的地址;否则返回NULL。

使用示例

int main()
{char arr1[] = "abcdefabcdef";char arr2[] = "def";char* ret = strstr(arr1, arr2);if(ret!=NULL){printf("%s\n", ret);}return 0;
}

输出结果: 

 strstr的模拟实现

这里使用的是BF算法来实现:

char* my_strstr(char* str1, char* str2)
{char* cp = str1;char* s1 = cp;char* s2 = str2;while (*cp){s1 = cp;s2 = str2;while (*s1 && *s2 && *s1 == *s2){s1++;s2++;}if (*s2 == '\0')return cp;cp++;}return NULL;
}


5.字符串分割strtok

  • sep参数是个字符串,定义了用作分隔符的字符集合
  • 第一个参数指定一个字符串,它包含了0个或者多个由sep字符串中一个或者多个分隔符分割的标 记。
  • strtok函数找到str中的下一个标记,并将其用 \0 结尾,返回一个指向这个标记的指针。(注: strtok函数会改变被操作的字符串,所以在使用strtok函数切分的字符串一般都是临时拷贝的内容并且可修改。)
  • strtok函数的第一个参数不为 NULL ,函数将找到str中第一个标记,strtok函数将保存它在字符串中的位置。
  • strtok函数的第一个参数为 NULL ,函数将在同一个字符串中被保存的位置开始,查找下一个标记。
  • 如果字符串中不存在更多的标记,则返回 NULL 指针。

使用示例1

int main()
{char arr[] = "123@qq.com";char sep[] = "@.";char copy[20];strcpy(copy, arr);char* pc = strtok(copy, sep);while (pc){puts(pc);pc = strtok(NULL, sep);}return 0;
}

使用示例2

int main()
{char arr[] = "123@qq.com";char sep[] = "@.";char copy[20];strcpy(copy, arr);char* ret = NULL;for (ret = strtok(copy, sep); ret != NULL; ret = strtok(NULL, sep)){printf("%s\n", ret);}return 0;
}

输出结果: 

6.错误信息报告

strerror

 返回错误码,所对应的错误信息。

库函数在执行的时候,发生了错误会将错误码存放在errno这个全局变量中。errno是C语言提供的一个全局变量。

下面代码是将错误码0~9所对应的错误信息给打印出来:

int main()
{int i = 0;for (int i = 0; i < 10; i++){printf("%d:%s\n", i, strerror(i));}return 0;
}

 当然,这里展示的仅仅是一小部分。

下面演示一下当我们进行文件操作是,打开文件失败后查找打开失败的原因:

int main()
{FILE* pf = fopen("data.txt", "r");if (pf == NULL){printf("%s\n", strerror(errno));}return 0;
}

程序输出了“ No such file or directory ”的错误提示,意思是没有要打开的文件。

perror

perror(str) 用来将上一个函数发生错误的原因输出到标准设备(stderr)。参数 str 所指的字符串会先打印出,后面再加上错误原因字符串。此错误原因依照全局变量errno的值来决定要输出的字符串。

使用示例

int main()
{FILE* pf = fopen("data.txt", "r");if (pf == NULL){printf("%s\n", strerror(errno));perror("fopen");}return 0;
}

 输出结果:


7.字符分类函数

8.字符转换函数

 下面代码的功能是输入一串字符,将字符串中的小写字母转成大写,大写字母转成小写

int main()
{char arr[30] = { 0 };gets(arr);char* p = arr;while (*p){if (isupper(*p)){*p = tolower(*p);}else if (islower(*p)){*p = toupper(*p);}*p++;}puts(arr);
}

 
9.内存操作函数

memcpy

  • 函数memcpy从source的位置开始向后复制num个字节的数据到destination的内存位置。
  • 这个函数在遇到 '\0' 的时候并不会停下来。
  • 如果source和destination有任何的重叠,复制的结果都是未定义的。

可以拷贝任意类型的数据:字符串,整形数组,结构体数据类型……

使用示例

int main()
{int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };int arr2[20] = { 0 };//将arr1的内容拷贝到arr2中memcpy(arr2, arr1, 40);for (int i = 0; i < 20; i++){printf("%d ", arr2[i]);}return 0;
}

 输出结果:

 memcpy的模拟实现

void* my_memcpy(void* dest, 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;
}

注意下面这种写法编译器会报错:

在代码中,使用了(char*)dest++(char*)src++来更新指针的位置。然而,这种写法是不正确的,因为后缀递增操作符++的优先级高于类型转换操作符(char*),所以实际上你只是在转换指针类型后递增了一个临时变量,而没有更新指针本身的值。

注意:mencpy 函数是用来处理不会重叠的内存拷贝,当我们拷贝两个相同的字符串是,可以结果与预期的会不相同:

int main()
{int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };my_memcpy(arr1+2, arr1, 32);for (int i = 0; i < 10; i++){printf("%d ", arr1[i]);}printf("\n");return 0;
}

对于上面的代码,我们的预期结果是这样的:

 实际输出的结果却是如下:

 如果要拷贝重叠的内存,我们就要使用下面这个函数了


memmove

  • 和memcpy的差别就是memmove函数处理的源内存块和目标内存块是可以重叠的。
  • 如果源空间和目标空间出现重叠,就得使用memmove函数处理。

使用示例

int main()
{int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };memmove(arr1+2, arr1, 32);for (int i = 0; i < 10; i++){printf("%d ", arr1[i]);}printf("\n");return 0;
}

 输出结果:

 memmove的模拟实现

不同于memcpy,mommove的拷贝需要分情况:

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{printf("从后向前拷贝\n");//从后向前拷贝while (num--){*((char*)dest + num) = *((char*)src + num);}}return ret;
}

 
memset

 memset是一个初始化函数,作用是将某一块内存中的全部设置为指定的值。

  • prr 指向要填充的内存块。
  • value 是要被设置的值。
  • num 是要被设置该值的字符数。
  • 返回类型是一个指向存储区s的指针。

使用示例1

int main()
{char arr[] = "hello world";memset(arr + 1, 'x', 4);printf("%s\n", arr);return 0;
}

上面代码的作用是将的arr数组的第2个元素到第5个元素的值设置成‘x',输出结果:

使用示例2

int main()
{int arr[10] = { 0 };memset(arr, 1, 10);return 0;
}

 
memcmp

比较从ptr1和ptr2指针开始的num个字节

返回值如下:

 使用示例

int main()
{int arr1[] = { 1,2,1,4,5,6 };int arr2[] = { 1,2,257 };int ret = memcmp(arr1, arr2, 9);printf("%d\n", ret);return 0;
}

对于上面的代码,可以借助编译器的调试来查看 arr1 和 arr2 的内存:

可以看出,arr1和arr2的前9个字节是相同的,而我们比较的是前9个字节,因此输出结果应该是0。 

 

http://www.lryc.cn/news/130366.html

相关文章:

  • 前馈神经网络正则化例子
  • spring的核心技术---bean的生命周期加案例分析详细易懂
  • 【Maven教程】(一)入门介绍篇:Maven基础概念与其他构建工具:理解构建过程与Maven的多重作用,以及与敏捷开发的关系 ~
  • 今天,谷歌Chrome浏览器部署抗量子密码
  • SUMO traci接口控制电动车前往充电站充电
  • 现代CSS中的换行布局技术
  • 简单理解Python中的深拷贝与浅拷贝
  • C++之std::pair<uint64_t, size_t>应用实例(一百七十七)
  • 前端打开后端返回的HTML格式的数据
  • How to deal with document-oriented data
  • Http 状态码汇总
  • mysql自定义实体类框架
  • 批量将Excel中的第二列内容从拼音转换为汉字
  • 消息推送:精准推送,提升运营效果,增添平台活力
  • [保研/考研机试] KY43 全排列 北京大学复试上机题 C++实现
  • Java将时间戳转化为特定时区的日期字符串
  • 【算法挨揍日记】day03——双指针算法_有效三角形的个数、和为s的两个数字
  • 通过 kk 创建 k8s 集群和 kubesphere
  • 感觉和身边其他人有差距怎么办?
  • 【C语言基础】宏定义的用法详解
  • 微服务系列文章之 SpringBoot 最佳实践
  • C++并发多线程--std::async、std::packaged_task和std::promise的使用
  • opencv-目标追踪
  • 【数据结构】 单链表面试题讲解
  • C++ string类的模拟实现
  • Qt实现简单的漫游器
  • 【c语言】文件操作
  • 【Unity】坐标转换经纬度方法(应用篇)
  • element时间选择器el-date-picter使用disabledDate指定禁用的日期
  • 出学校干了 5 年外包,已经废了