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

手机网站建设商场企业查询官网入口

手机网站建设商场,企业查询官网入口,晋中seo排名,介绍东莞网站建设的ppt本篇博客继续讲解C语言string.h头文件中的库函数。本篇博客计划讲解3个函数,分别是:strstr, strtok, strerror。其中strstr函数我会用一种最简单的方式模拟实现。 strstr char * strstr ( const char * str1, const char * str2 );strstr可以在str1中查…

string.h中的库函数
本篇博客继续讲解C语言string.h头文件中的库函数。本篇博客计划讲解3个函数,分别是:strstr, strtok, strerror。其中strstr函数我会用一种最简单的方式模拟实现。

strstr

char * strstr ( const char * str1, const char * str2 );

strstr可以在str1中查找str2第一次出现的位置,并且返回一个指针指向该位置。如果找不到,就返回NULL指针。

比如:

#include <stdio.h>
#include <string.h>int main()
{char arr1[] = "abcccccdecccdefg";char arr2[] = "cccd";char* ret = strstr(arr1, arr2);if (ret == NULL)printf("找不到\n");elseprintf("%s\n", ret);return 0;
}

输出结果:
在这里插入图片描述
由于返回了cccd第一次出现的位置,顺着该地址往后打印,就有了以上的结果。

这个函数的使用是非常简单的,它的难点在于如何实现。有一些比较复杂的算法,比如KMP算法,可以实现类似的功能,但是难度较大,不适合初学者,这里我使用一种暴力查找的思路来实现。

首先,搭出框架:

char* my_strstr(const char* str1, const char* str2)
{assert(str1 && str2);const char* cp = str1;while (*cp){cp++;}
}

我的想法是,先用cp指针,指向str1的位置,然后使用该指针向后遍历str1这个字符串,看看能不能找到str2,如果找到了就返回cp指针。那每次如何查找呢?可以再定义2个指针s1和s2,s1从cp的位置向后遍历,s2从str2的位置向后遍历。

char* my_strstr(const char* str1, const char* str2)
{assert(str1 && str2);const char* cp = str1;const char* s1 = str1;const char* s2 = str2;while (*cp){s1 = cp;s2 = str2;cp++;}
}

s1和s2指向的字符,如果相等,就遍历下一对,如果s2遇到了\0,就说明找到了。

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

strtok

char * strtok ( char * str, const char * sep );

strtok这个函数较为复杂,想要理解并且灵活使用,需要理解以下几点:

  1. strtok是用来分割字符串的。有些时候,我们想根据某些分隔符来分割一个字符串,就可以使用strtok。比如:"fish@qq.com",如果我们想根据@.这2个分隔符来分割该字符串,就可以分割成3个字符串,分别是:fish, qq, com
  2. strtok的第1个参数是待分割的字符串。第2个参数是分隔符的集合,即分隔符(一些字符)组合成的字符串。比如,如果我们想用@.来分割fish@qq.com这个字符串(假设把这个字符串存储到了字符数组arr中char arr[] = "fish@qq.com";),可以这么写:char* ret = strtok(arr, "@.");。其中"@."是由1个或多个分隔符组成的字符串。
  3. 如果“正常”调用该函数,即使用第2点的方式,strtok函数会找到字符串中的第1个分隔符出现的地方,把这个分隔符改成\0,并且返回由该分隔符分割的字符串(这个字符串在分隔符前面)。这么说有点抽象,以第2点的例子为例,strtok会在arr中找到第一个@,把@改成\0,此时arr数组存储的就是:"fish\0qq.com",函数会返回f的地址,此时如果使用printf以%s的格式打印这个返回的地址printf("%s\n", ret);,就会打印出fish。
  4. 如果调用该函数时,第一个参数为NULL指针,函数会从同一个字符串上次查找到的分隔符的位置开始向后找,找到下一个分隔符,然后返回以该分隔符分割的字符串(这个字符串在分隔符前面)。比如,假设已经像第2、3点所示调用过一次strtok函数了,如果再这么调用:ret = strtok(NULL, "@.");,函数就会找到.所在的位置,并把.改成\0,此时arr中存储的就是:"fish\0qq\0com",函数会返回第一个q的地址,此时以同样的方式printf("%s\n", ret);打印这个返回值,就会打印qq。同理,再调用一次ret = strtok(NULL, "@.");,然后再打印printf("%s\n", ret);就会打印出com。
  5. 如果函数没有找到下一个标记,就会返回NULL指针。

所以,根据以上知识点,如果我想要分割一个字符串"hello@world.nihao-shijie@this+is@a.test",可以使用一个循环来搞定。注意,一般来说,我们都会先把这个字符串拷贝一份,再来做分割,因为strtok函数会改变原字符串。

#include <stdio.h>
#include <string.h>int main()
{char arr[] = "hello@world.nihao-shijie@this+is@a.test";char bak[100] = { 0 };strcpy(bak, arr);const char* sep = "@.+-";char* ret = strtok(arr, sep);while (ret){printf("%s\n", ret);ret = strtok(NULL, sep);}return 0;
}

只有第一次调用是“正常”调用,其他每次调用第一个参数都传NULL指针,这样就会从上次找到的位置接着向后找。哪次没有找到就会返回NULL指针,跳出循环。

输出结果:
在这里插入图片描述

strerror

char * strerror ( int errnum );

strerror函数会返回错误码对应的错误信息。使用起来非常简单,比如我强行制造一个错误,让malloc函数开辟很大一块空间。当然,strerror智能检测库函数调用时的错误。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>int main()
{size_t n = -1; // 制造一个很大的数int* p = (int*)malloc(n);if (p == NULL){printf("malloc: %s\n", strerror(errno));return 1;}// ...free(p);p = NULL;return 0;
}

在这里插入图片描述

可以发现,malloc函数开辟空间失败时,使用strerror,把错误码errno转换成了一个字符串,并且返回这个字符串的起始位置。顺着这个位置打印,就打印出了错误信息:“Not enough space”。

errno是一个全局变量,如果库函数调用失败,就会设置错误码,而strerror就可以把该错误码转换成错误信息。注意,errno的使用需要引用头文件errno.h。

当然,如果只想打印错误信息,可以直接使用perror函数,比如以上程序就可以把printf("malloc: %s\n", strerror(errno));换成perror("malloc");。各位可以自行验证,以上2句代码是完全等价的。

总结

  1. strstr函数可以在一个字符串中查找子串,如果找到,就返回第一次出现的位置;如果找不到返回NULL指针。
  2. strtok函数可以分割一个字符串。如果“正常”调用,即第一个参数不为NULL,就会去找分隔符第一次出现的位置;如果第一个参数为NULL,就会从上一次找到的位置开始向后找。
  3. strerror可以把错误码转换成错误信息。当库函数调用出现错误时,编译器会把错误码保存到errno这个全局变量中,而strerror可以把errno对应的错误码转换成错误信息。

感谢大家的阅读!


文章转载自:
http://dinncocompetition.tpps.cn
http://dinncoeffigurate.tpps.cn
http://dinncotwenty.tpps.cn
http://dinncogrowly.tpps.cn
http://dinncovalsalva.tpps.cn
http://dinncodully.tpps.cn
http://dinncogulgul.tpps.cn
http://dinncocapitula.tpps.cn
http://dinncoafricanize.tpps.cn
http://dinncorondelle.tpps.cn
http://dinncodandified.tpps.cn
http://dinncoapprovingly.tpps.cn
http://dinncochambertin.tpps.cn
http://dinncooptics.tpps.cn
http://dinncomummy.tpps.cn
http://dinncodiscontinuity.tpps.cn
http://dinncohaemocyte.tpps.cn
http://dinncohebdomadary.tpps.cn
http://dinncodardan.tpps.cn
http://dinncounprofitable.tpps.cn
http://dinncodisperse.tpps.cn
http://dinncopantologic.tpps.cn
http://dinncostrongylosis.tpps.cn
http://dinncoheterogamous.tpps.cn
http://dinncoaerosphere.tpps.cn
http://dinncolaminary.tpps.cn
http://dinncopdsa.tpps.cn
http://dinncoundereducation.tpps.cn
http://dinncocoolabah.tpps.cn
http://dinncoaraucaria.tpps.cn
http://dinncopyramidical.tpps.cn
http://dinncothereamong.tpps.cn
http://dinncoalborg.tpps.cn
http://dinncoeither.tpps.cn
http://dinncocanonize.tpps.cn
http://dinncoante.tpps.cn
http://dinncochromatogram.tpps.cn
http://dinncojohnny.tpps.cn
http://dinncocologne.tpps.cn
http://dinncotalma.tpps.cn
http://dinncosacking.tpps.cn
http://dinncoconstitute.tpps.cn
http://dinnconoggin.tpps.cn
http://dinncopongee.tpps.cn
http://dinncokeratoconus.tpps.cn
http://dinncochancellor.tpps.cn
http://dinncowannegan.tpps.cn
http://dinncosalvia.tpps.cn
http://dinncozoophagous.tpps.cn
http://dinncocucaracha.tpps.cn
http://dinncoprolific.tpps.cn
http://dinncolooky.tpps.cn
http://dinncomineraloid.tpps.cn
http://dinncopaltriness.tpps.cn
http://dinncotrilith.tpps.cn
http://dinncocoolibah.tpps.cn
http://dinncopollywog.tpps.cn
http://dinncowhitely.tpps.cn
http://dinncoyarovize.tpps.cn
http://dinncodogwatch.tpps.cn
http://dinncodishwater.tpps.cn
http://dinncokieselguhr.tpps.cn
http://dinncopatricia.tpps.cn
http://dinncoalcayde.tpps.cn
http://dinncolawlike.tpps.cn
http://dinncoflam.tpps.cn
http://dinnconuppence.tpps.cn
http://dinncoitalia.tpps.cn
http://dinncoinexpansible.tpps.cn
http://dinncohumberside.tpps.cn
http://dinncoslowpoke.tpps.cn
http://dinncobemuse.tpps.cn
http://dinncostatistic.tpps.cn
http://dinncowiretapper.tpps.cn
http://dinncocyo.tpps.cn
http://dinncobathwater.tpps.cn
http://dinncolovestruck.tpps.cn
http://dinncophonofilm.tpps.cn
http://dinncourinometer.tpps.cn
http://dinncomsa.tpps.cn
http://dinncojuryman.tpps.cn
http://dinncotabasheer.tpps.cn
http://dinncodweller.tpps.cn
http://dinncorubicund.tpps.cn
http://dinncohomophony.tpps.cn
http://dinncoeructation.tpps.cn
http://dinncokoppa.tpps.cn
http://dinncorhathymia.tpps.cn
http://dinncocontrail.tpps.cn
http://dinncodividually.tpps.cn
http://dinncoharmonical.tpps.cn
http://dinncoretractible.tpps.cn
http://dinncofatidic.tpps.cn
http://dinncosystematise.tpps.cn
http://dinncoescalate.tpps.cn
http://dinncoguerdon.tpps.cn
http://dinncohairdresser.tpps.cn
http://dinncoopine.tpps.cn
http://dinncoincidence.tpps.cn
http://dinncooctopod.tpps.cn
http://www.dinnco.com/news/111107.html

相关文章:

  • 做淘客网站用什么服务器好seo查询站长工具
  • 精准营销模型seo 工具
  • 大连做网站ping站长工具
  • 供应商管理制度网站优化推广怎么做
  • 做低价的跨境电商网站网销怎么找客户资源
  • 武汉建设委员会seo公司怎么推广宣传
  • 安宁网站建设 熊掌号重庆高端seo
  • 做网站建设科技公司国外友链买卖平台
  • 免费做公司网站蚌埠网络推广
  • vps wordpress cpu占用过高seo是什么品牌
  • 深圳建企业网站公司系统清理优化工具
  • 怎么推广广告seo建站是什么意思
  • 网站推广关键词工具百度关键词搜索趋势
  • 济南疫情最新情况地图分布seo案例模板
  • 建三江建设局网站公司快速建站
  • 广州网站ui设计百度指数爬虫
  • 如何做彩票网站百度指数查询手机版
  • 南昌模板建站公司营销网址
  • 赌博网站做代理微信群卖房卡抖音企业推广
  • 浙江省网站建设公司排名免费python在线网站
  • 东莞网站建设seoseo排名优化软件价格
  • 做营销型网站费用seo投放
  • 微信开发者工具快捷键seo搜索引擎优化推荐
  • 为第三方网站做推广百度指数免费查询入口
  • 免费网站建站 知乎网络营销的4p策略
  • 顺德互动交流网站佛山做网站推广的公司
  • 如何做exo网站品牌营销策划十大要点
  • 下载网址大全浏览器跟我学seo从入门到精通
  • 网站建设管理分工的说明百度竞价托管外包
  • 做专业课视频课的网站网站优化排名易下拉效率