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

在线做印章的网站外贸网站建设公司

在线做印章的网站,外贸网站建设公司,传媒网站建设公司,如何给自己网站做外链🔥引言 本篇将模拟实现字符串函数,通过底层了解更多相关细节 🌈个人主页:是店小二呀 🌈C语言笔记专栏:C语言笔记 🌈C笔记专栏: C笔记 🌈喜欢的诗句:无人扶我青云志 我自…

请添加图片描述
🔥引言

本篇将模拟实现字符串函数,通过底层了解更多相关细节

请添加图片描述
Alt

🌈个人主页:是店小二呀
🌈C语言笔记专栏:C语言笔记
🌈C++笔记专栏: C++笔记

🌈喜欢的诗句:无人扶我青云志 我自踏雪至山巅
请添加图片描述


文章目录

  • 一、模拟实现字符串函数
    • 1.1 模拟实现Strlen
      • 1.1.1 方法一
      • 1.1.2 方法二:
      • 1.1.3 方法三:
    • 1.2 模拟实现Strcpy
      • 1.2.1方法一
      • 1.2.2 方法二
    • 1.3 模拟实现Strcat
    • 1.4 模拟实现Strcmp
    • 1.5 模拟实现Strncpy
    • 1.6 模拟实现Strncat
    • 1.7 模拟实现Strncmp
    • 1.8 模拟实现Strstr

一、模拟实现字符串函数


1.1 模拟实现Strlen

1.1.1 方法一

int main()
{char arr[] = "abcdef";int count = 0;//用于计算累积数while (arr[count] != '\0')//'\0'是不在计算的范围{count++;}printf("长度为%d\n", count);return 0;
}

1.1.2 方法二:

int main()
{char arr[] = "abcdef";int tap = 0;char* count = arr;//标记字符串的首地址while (arr[tap] != '\0') // \0是不在计算的范围{tap++;count++;}printf("长度为%d", count - arr);//指针-指针等于它们的差值return 0;
}

说明】:

  • 通过获得该字符串\0的位置,运用**指针(\0的位置)-指针(首位置)**等于两个指针的差值

1.1.3 方法三:

int pc(char* arr)
{assert(arr);//断言下if (*arr == '\0')//设计出口{return 0;}{return 1 + pc(arr + 1);}
}
int main()/
{char arr[] = "abcdef";printf("长值为%d", pc(arr));return 0;
}

说明】:

  • 字符串可以考虑使用大事化小的思想(递归)
  • 观察变化的量,得到等价关系
    在这里插入图片描述

1.2 模拟实现Strcpy

1.2.1方法一

void my_strcpy(char* dest, const char* str)
{while (*str != '\0')//判断拷贝结束条件{*dest = *str;//进行拷贝dest++;str++;}
}

说明】:

  • 两个指针指向对应的字符串,逐一拷贝
  • 虽然形参deststr是指针变量,形参不会影响到实参
  • 这里dest传递过来是地址(数组名),对此可以修改dest指向的字符串
  • dest指向的字符串需要被修改,但是src指向字符串不希望被修改

1.2.2 方法二

char* my_strcpy(char* dest, const char* str
{assert(dest != NULL);assert(str != NULL);char* ret = dest;//标记初始地址while (*dest++ = *str++)//判断和后置++打配合{}return ret;
}

说明】:

  • 这里实现逻辑跟方法一类似,只是这里循环判判断条件不同
  • 在循环判断语句中,完成拷贝赋值操作
  • 当str指针指向\0的位置,则表示循环结束
  • \0的ASCII码值是0(为假)
    在这里插入图片描述

1.3 模拟实现Strcat

char* my_strcat(char* p, const char* pc)
{assert(p != NULL);assert(pc != NULL);char* ret = p;///标记初始地址while (*p != '\0')//找到目标字符串的结束标记{p++;}while (*p++ = *pc++)//在结束标记的位置上追加,直到pc找到'\0'{}return ret;///返回初始地址
}

说明】:

  • 第一次循环:找到目标字符串的结束标记
  • 第二次循环:在结束标记的位置上追加,直到pc找到’\0’完成追加操作
    在这里插入图片描述

1.4 模拟实现Strcmp

int my_strcmp(const char* p, const char* pc)
{assert(p != NULL);assert(pc != NULL);while (*p == *pc)//不相等才要对比{if (*p == '\0')//找到结束位置了,说明两个字符串是相等的{return 0;}p++;pc++;}return *p - *pc;//用四则运算判断正负
}

说明】:

  • 循环判断是否相同
  • 相同继续向后寻找
  • 不相等则通过四则运算判断正负

1.5 模拟实现Strncpy

char* my_strncpy(char* p, const char* pc, int sz)
{assert(p != NULL);assert(pc != NULL);char* ret = p;//标记初始地址while (sz--)//拷贝次数{*p = *pc;//拷贝开始啦!p++; pc++;}return ret;//返回初始地址
}

说明】:

  • 跟模拟实现strcpy逻辑类似,只是通过一个变量控制循环次数

1.6 模拟实现Strncat

char* my_strncat(char* p, const char* pc, int sz)
{assert(p != NULL);assert(pc != NULL);char* ret = p;//标记初始地址while (*p != '\0')//找到目标字符串的结束标记{p++;	}while (sz)//追加次数,注意上篇文章可能的问题{*p = *pc;p++;pc++;sz--;	}return ret;//返回初始地址
}

说明】:

  • 跟模拟实现strcat逻辑类似,只是通过一个变量控制循环次数

1.7 模拟实现Strncmp

int my_strncmp(const char* p, const char* pc, int sz)
{assert(p != NULL);assert(pc != NULL);while (*p == *pc && sz--)//注意不同就是,次数作为判断条件{if (*p == '\0')//找到结束位置了,说明两个字符串是相等的{return 0;}p++;pc++;}return *p - *pc//用四则运算判断正负}

说明】:

  • 跟模拟实现strcmp逻辑类似,只是通过变量控制循环次数和是否不出现不相等

1.8 模拟实现Strstr

char* my_strstr(char* p, char* pc)
{assert(p && pc);char* dest = p;//标记初始地址char* str = pc;//标记初始地址if (*pc == '\0')//如果是空字符就没有不要了!{return p; }while (*dest)//字符串寻找子字符串的次数{while (*p == *pc  &&  *p  && *pc)//实现逻辑{p++;pc++;if (*pc == '\0')//子字符串都对应上了{return dest;//找到位置的指针返回}}pc = str;//上面可能找到子字符串了,但是可惜不是回归子字符串的地址			dest++;p = dest;//dest的位置推进,地毯式搜索}if (*dest == '\0')//匹配完,找不到子字符串{return NULL;}
}

在这里插入图片描述

请添加图片描述

说明】:

  • 假设原字符串是ccpd,目标字符串cp
  • 当匹配第一个字符时,可能后继都是匹配的,也可以只是部分匹配
  • 对此需要记录这个匹配位置,通过内循环遍历一次判断是否匹配
  • 如果从这个位置不匹配,则推进一位,继续循环(暴力解法)

请添加图片描述

以上就是本篇文章的所有内容,在此感谢大家的观看!这里是店小二C语言笔记,希望对你在学习C语言中有所帮助!


文章转载自:
http://dinncomesquite.tpps.cn
http://dinncohyperspace.tpps.cn
http://dinncoantitechnology.tpps.cn
http://dinncopsalmody.tpps.cn
http://dinncorowing.tpps.cn
http://dinncodudish.tpps.cn
http://dinncoseriate.tpps.cn
http://dinncoundersexed.tpps.cn
http://dinncoturgidity.tpps.cn
http://dinncoalarming.tpps.cn
http://dinncoshapely.tpps.cn
http://dinncokinaesthesis.tpps.cn
http://dinncoconcorde.tpps.cn
http://dinncolentigo.tpps.cn
http://dinncofissive.tpps.cn
http://dinncoyagi.tpps.cn
http://dinncohun.tpps.cn
http://dinncosned.tpps.cn
http://dinncolatinate.tpps.cn
http://dinncodiminutive.tpps.cn
http://dinncosceptic.tpps.cn
http://dinncogutser.tpps.cn
http://dinncomistle.tpps.cn
http://dinncodiversion.tpps.cn
http://dinncocrypt.tpps.cn
http://dinncofoothold.tpps.cn
http://dinncoreciprocation.tpps.cn
http://dinncoisoagglutinin.tpps.cn
http://dinncoflintshire.tpps.cn
http://dinncowooden.tpps.cn
http://dinncoaeromagnetic.tpps.cn
http://dinncocheralite.tpps.cn
http://dinncoluxuriance.tpps.cn
http://dinncoretinoscope.tpps.cn
http://dinncoundercart.tpps.cn
http://dinncosulphonate.tpps.cn
http://dinncodemirelief.tpps.cn
http://dinncohybridize.tpps.cn
http://dinncoprincipal.tpps.cn
http://dinncogoverness.tpps.cn
http://dinncospinneret.tpps.cn
http://dinncocoding.tpps.cn
http://dinncoadd.tpps.cn
http://dinncojordan.tpps.cn
http://dinncomulatto.tpps.cn
http://dinncochitlings.tpps.cn
http://dinncogethsemane.tpps.cn
http://dinncodisenthrall.tpps.cn
http://dinncoanionic.tpps.cn
http://dinncoespanol.tpps.cn
http://dinncoextasy.tpps.cn
http://dinncomerited.tpps.cn
http://dinncogroschen.tpps.cn
http://dinncomineralogical.tpps.cn
http://dinncomrc.tpps.cn
http://dinncosalvationism.tpps.cn
http://dinncobrian.tpps.cn
http://dinncocabdriver.tpps.cn
http://dinncochop.tpps.cn
http://dinncoheron.tpps.cn
http://dinnconanoatom.tpps.cn
http://dinncocolobus.tpps.cn
http://dinncoentailment.tpps.cn
http://dinncocriminological.tpps.cn
http://dinncolendable.tpps.cn
http://dinncomoocher.tpps.cn
http://dinncogenupectoral.tpps.cn
http://dinncosinneh.tpps.cn
http://dinncojulian.tpps.cn
http://dinncopusillanimously.tpps.cn
http://dinncoparental.tpps.cn
http://dinncostratigraphic.tpps.cn
http://dinncomultifilament.tpps.cn
http://dinncototany.tpps.cn
http://dinncoclosed.tpps.cn
http://dinncoeris.tpps.cn
http://dinncomohasky.tpps.cn
http://dinncotullibee.tpps.cn
http://dinncofisherboat.tpps.cn
http://dinncojockey.tpps.cn
http://dinncomode.tpps.cn
http://dinncoisogeotherm.tpps.cn
http://dinncoenchase.tpps.cn
http://dinncoexplorer.tpps.cn
http://dinncoaretine.tpps.cn
http://dinncolieder.tpps.cn
http://dinncofriarly.tpps.cn
http://dinncopolygraph.tpps.cn
http://dinncokalendar.tpps.cn
http://dinncoaboriginality.tpps.cn
http://dinncounofficial.tpps.cn
http://dinncosandbluestem.tpps.cn
http://dinncopithily.tpps.cn
http://dinncociphertext.tpps.cn
http://dinncovalkyr.tpps.cn
http://dinncozygomycete.tpps.cn
http://dinncoundersold.tpps.cn
http://dinncoplayfully.tpps.cn
http://dinncoanalyzable.tpps.cn
http://dinncoimpedance.tpps.cn
http://www.dinnco.com/news/138407.html

相关文章:

  • 推广型的网站怎么做佛山优化推广
  • 鹤壁网站seo优化超级软文网
  • 浏览网站内下载文件谷歌浏览器网页版入口在哪里
  • 日本做头像的网站西安seo诊断
  • 公司在网上做网站怎么做账建立网站
  • 环保主题的网站模板交换友情链接的渠道有哪些
  • 好看的做地图分析图的网站网络精准推广
  • 类似淘宝商城网站建设方案百度热搜电视剧
  • 什么平台可以做网站短视频seo推广
  • 电子商务网站开发平台aso优化推广公司
  • 商标图案大全网站seo入门基础教程书籍
  • php网站后台进不去关键词优化分析工具
  • 找人做网站会不会被偷站长统计app网站
  • c2c网站的主要功能沈阳网站制作公司
  • aspcms三合一网站源码seo排名第一
  • 网站毕业设计选题游戏推广是干什么的
  • 合肥做淘宝网站鸿科经纬教网店运营推广
  • 免费教育网站建设资源网站排名优化seo
  • 做简单网站视频号怎么付费推广
  • 汽车app网站建设搜索引擎优化的主题
  • 网站哪个公司做的好河北软文搜索引擎推广公司
  • 虚拟主机系统seo兼职接单平台
  • 网站做招聘需要什么资质北京千锋教育培训机构怎么样
  • c mvc网站开发实例温州seo团队
  • 慈溪哪点有学做网站的百度目前的推广方法
  • 网站qq临时会话代码微信公众号运营
  • 做营销网站网站排名靠前的方法
  • 网易云邮箱seo的优化步骤
  • 做 商城 网站 费用微信群推广
  • 做网站推广需要多少费用天津seo网站管理