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

个人或主题网站建设实验报告网站开发商城实例

个人或主题网站建设实验报告,网站开发商城实例,安康seo,软件开发和大数据哪个前景好引言 引言 下面介绍会有一些英文文档,给标了注释,要多练习读英文文档 1. 字符分类函数 2. 字符转换函数 3. strlen 的使用和模拟实现 4. strcpy 的使用和模拟实现 5. strcat 的使用和模拟实现 6. strcmp 的使用和模拟实现 7. strncpy 函数…

引言

引言

        下面介绍会有一些英文文档,给标了注释,要多练习读英文文档

1. 字符分类函数

2. 字符转换函数

3. strlen    的使用和模拟实现

4. strcpy   的使用和模拟实现

5. strcat    的使用和模拟实现

6. strcmp  的使用和模拟实现

7. strncpy 函数的使用

8. strncat  函数的使用

9. strncmp函数的使用

10. strstr   的使用和模拟实现

11. strtok  函数的使用

12. strerror函数的使用

1.字符串分类函数

2.字符串转换函数

         这篇博文里面有详解:

C/C++中对字符处理的常用函数-CSDN博客文章浏览阅读533次,点赞13次,收藏19次。C语言中的 ctype.h 头文件提供了一系列字符分类和转换函数,用于高效处理字符相关操作。 https://blog.csdn.net/2401_88433210/article/details/146140744?spm=1011.2415.3001.10575&sharefrom=mp_manage_link

3.strlen 的使用和模拟实现

函数原型:strlen

size_t strlen ( const char * str );

• 字符串以 '\0' 作为结束标志,strlen函数返回的是在字符串中 '\0' 前面出现的字符个数(不包含 '\0' )。

• 参数指向的字符串必须要以 '\0' 结束。

• 注意函数的返回值为size_t,是无符号的(易错)

• strlen的使用需要包含头文件 <string.h>

参考代码:

#include <stdio.h>
#include <string.h>
int main()
{const char* str1 = "abcdef";const char* str2 = "bbb";if (strlen(str2) - strlen(str1) > 0){printf("str2>str1\n");}else{printf("srt1>str2\n");}return 0;
}

• 学会strlen函数的模拟实现 (重点)

strlen函数的模拟

方式一:计数器方式

参考代码:

int my_strlen(const char* str)
{int count = 0;    assert(str); //若str为空指针,终止程序并报错while (*str)  // \n  等价于 0;{count++;str++;}return count;
}

方式二:递归

参考代码:


int my_strlen(const char* str) 
{assert(str);if (*str == '\0')  return 0;    //  到\0位置时停止递归,向上返回。elsereturn 1 + my_strlen(str + 1);  
}

方式三:指针 - 指针的方式

参考代码: 

int my_strlen(char* s)
{assert(str);char* p = s;while (p != '\0')p++;return p - s;
}

4. strcpy 的使用和模拟实现

函数原型:strcpy - C++ Reference

char* strcpy(char * destination, const char * source );

 • 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' 拷贝到目标空间。

• 目标空间必须足够大,以确保能存放源字符串。

• 目标空间必须可修改。

• 学会模拟实现(重点)

strcpy函数的模拟

//1.参数顺序
//2.函数的功能,停⽌条件
//3.assert
//4.const修饰指针
//5.函数返回值
//6.题⽬出⾃《⾼质量C / C++编程》书籍最后的试题部分char* my_strcpy(char* dest, const char* src)
{char* ret = dest;assert(dest != NULL);assert(src != NULL);while ((*dest++ = *src++)){;}return ret;
}

5.strcat函数的使用和模拟实现

函数原型:strcat - C++ Reference

 char * strcat(char *dest, const char*src);

• 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' 结束。

• 目标字符串中也得有 \0 ,否则没办法知道追加从哪里开始。

• 目标空间必须有足够的大,能容纳下源字符串的内容。

• 目标空间必须可修改。

• 该函数不能实现自己给自己追加。

strcat函数的模拟实现:

char* my_strcat(char* dest, const char* src)
{char* ret = dest;assert(dest != NULL);assert(src != NULL);while (*dest){dest++; //找到要目标字符串的'\0'的位置}while ((*dest++ = *src++)) //这里是简写了,就是把源字符串一个一个放到目标字符串的后面                                      {                          //当放'\0'的时候两边都是0,while循环结束                  ;  //空语句}return ret;
}

看代码注释。

6. strcmp  的使用和模拟实现

函数原型:strcmp - C++ Reference

 int strcmp (const char * str1, const char * str2);

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

◦ 那么如何判断两个字符串?比较两个字符串中对应位置上字符ASCII码值的大小。

 strcmp  函数的模拟实现:

int my_strcmp(const char* str1, const char* str2)
{int ret = 0;assert(str1 != NULL);assert(str2 != NULL);while (*str1 == *str2){if (*str1 == '\0')return 0; 说明两个字符串想等,返回0即可str1++;str2++;}return *str1 - *str2;
}

7. strncpy 函数的使用

函数原型:strncpy - C++ Reference

 char * strncpy ( char * destination, const char * source, size_t num );

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

参考代码:

#include <stdio.h>
#include <string.h>
int main()
{char str1[20];char str2[20] = "hello word";strcpy(str1, "To be   ");strncpy(str1, str2, 7);printf("%s\n", str1);return 0;
}

把str2里面前7个字符,复制到str1里面。

运行结果: 

8. strncat 函数的使用

函数原型:strncat - C++ Reference

char * strncat ( char * destination, const char * source, size_t num );

• Appends  the  first  num  characters  of  source  to  destination, plus a terminating null-character. (将source指向字符串的前num个字符追加到destination指向的字符串末尾,再追加⼀个\0 字符)。

• If  the  length  of  the  C  string  in  source   is less than num , only  the content  up to the  terminating null-character is copied.(如果source 指向的字符串的⻓度⼩于num的时候,只会将字符串中到 \0 的内容追加到destination指向的字符串末尾)。 

 参考代码:

/* strncat example */
#include <stdio.h>
#include <string.h>
int main()
{char str1[20];char str2[20];strcpy(str1, "To be ");strcpy(str2, "or not to be");strncat(str1, str2, 6);printf("%s\n", str1);return 0;
}

将字符串str2 里面 6 个字符复制到str1 里面。 

运行结果:

9.strncmp函数的使用

函数原型:strncmp - C++ Reference

 int strncmp ( const char * str1, const char * str2, size_t num );

        比较str1和str2的前num个字符,如果相等就继续往后比较,最多比较num个字母,如果提前发现不⼀ 样,就提前结束,大的字符所在的字符串大于另外⼀个。如果num个字符都相等,就是相等返回0. 

参考代码:

#include<stdio.h>
#include<string.h>int main()
{ char arr1[20] = "abcd";char arr2[20] = "abcdef";if (strncmp(arr1, arr2, 4) > 0)printf("arr1 > arr2");else if (strncmp(arr1, arr2, 4) < 0)printf("arr2 > arr1");else  if (strncmp(arr1, arr2, 4) == 0)printf("arr1 == arr2");return 0;
}

很显然 按字典序比较 arr1 是小于 arr2 的,但是又因为是比较前四个字符,所以arr1 == arr2; 

运行结果:

10. strstr 的使用和模拟实现

函数原型:strstr - C++ Reference

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

        Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1. (函数返回字符串str2在字符串str1中第⼀次出现的位置)。         

        The  matching  process  does  not  include  the  terminating null-characters, but it stops there.(字符串的比较匹配不包含 \0 字符,以 \0 作为结束标志)。 

参考代码:

/* strstr example */
#include <stdio.h>
#include <string.h>
int main()
{char str[] = "This is a simple string";char* pch;pch = strstr(str, "simple");strncpy(pch, "aaaaa", 2);printf("%s\n", str);return 0;
}

 运行结果:

 strstr的模拟实现:

char* strstr(const char* str1, const char* str2)
{char* cp = (char*)str1;char* s1, * s2;if (!*str2)return((char*)str1);while (*cp){s1 = cp;s2 = (char*)str2;while (*s1 && *s2 && !(*s1 - *s2)) //*s1 和 *s2相等,就while循环s1++, s2++;if (!*s2)return(cp);cp++;}return(NULL);
}

        定义两个指针s1,s2,s1先不动,让s2向后遍历比较, 找到符合题意的就return,找不到,就让s1往后走,再遍历s2,重复着往后走,直到找到,与s2相等的字符串,找不到就返回NULL。

11. strtok函数的使用(可能你看得很懵逼,多想想)(有点难度)

函数原型:strtok - C++ Reference

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

• sep参数指向⼀个字符串,定义了用作分隔符的字符集合

• 第⼀个参数指定⼀个字符串,它包含了0个或者多个由sep字符串中⼀个或者多个分隔符分割的标记。

• strtok函数找到str中的下⼀个标记,并将其用 \0 结尾,返回⼀个指向这个标记的指针。(注: strtok函数会改变被操作的字符串,所以被strtok函数切分的字符串⼀般都是临时拷⻉的内容并且可修改。)

• strtok函数的第⼀个参数不为NULL,函数将找到str中第⼀个标记,strtok函数将保存它在字符串中的位置。

• strtok函数的第⼀个参数为 NULL ,函数将在同⼀个字符串中被保存的位置开始,查找下⼀个标 记。

• 如果字符串中不存在更多的标记,则返回 NULL 指针。

参考代码: 

#include <stdio.h>
#include <string.h>
int main()
{char arr[] = "192.168.6.111";char* sep = ".";char* str = NULL;                 //第一次找到 . 后会将其设置为NULL,for (str = strtok(arr, sep); str != NULL; str = strtok(NULL, sep)){printf("%s\n", str);}printf("%s", arr);return 0;
}

12 strerror 函数的使用

函数原型:

char* strerror ( int errnum );

strerror 函数可以把参数部分错误码对应的错误信息的字符串地址返回来。 

        在不同的系统和C语言标准库的实现中都规定了⼀些错误码,⼀般是放在 errno.h 这个头文件中说明的,C语言程序启动的时候就会使用⼀个全局的变量errno来记录程序的当前错误码,只不过程序启动的时候errno是0,表示没有错误,当我们在使用标准库中的函数的时候发生了某种错误,就会将对应的错误码,存放在errno中,而⼀个错误码的数字是整数很难理解是什么意思,所以每⼀个错误码都是有对应的错误信息的。strerror函数就可以将错误对应的错误信息字符串的地址返回。 

参考代码1: 打印一下 0~10 这些错误码对应的信息

#include <errno.h>
#include <string.h>
#include <stdio.h>int main()
{int i = 0;for (i = 0; i <= 10; i++) {printf("%d :  %s\n",i, strerror(i));}return 0;
}

在Windows11+VS2022环境下输出的结果如下: 

 参考代码2:体验一下报错

#include <stdio.h>
#include <string.h>
#include <errno.h>
int main()
{//打开文件//打开文件失败的时候,会返回NULL指针//打开文件成功的时候,返回的是非NULL指针FILE* pFile;  //文件指针变量pFile = fopen("text.txt", "r");  //以读的形式打开text,txt文件if (pFile == NULL)printf("Error opening file unexist.ent: %s\n", strerror(errno));return 0;
}

 运行结果:

13.perror函数 

        学习一下 perror函数,perror函数相当于⼀次将上述代码中的第12行完成了(printf语句),直接将错误信息打印出来。perror函数打印完参数部分的字符串后,再打印⼀个冒号和⼀个空格,再打印错误信息。 

参考代码:

#include <stdio.h>
#include <string.h>
#include <errno.h>
int main()
{//打开文件//打开文件失败的时候,会返回NULL指针//打开文件成功的时候,返回的是非NULL指针FILE* pFile;  //文件指针变量pFile = fopen("text.txt", "r");  //以读的形式打开text,txt文件if (pFile == NULL)perror("text.txt");return 0;
}

运行结果:

 点赞拿图:


文章转载自:

http://iQSlrC8G.qpqwd.cn
http://ivpG7tCi.qpqwd.cn
http://ChCAE7Wf.qpqwd.cn
http://ZTUmj3Fy.qpqwd.cn
http://1Ip4ZIrk.qpqwd.cn
http://asGIPG3I.qpqwd.cn
http://sksWJMOR.qpqwd.cn
http://qkt7Vf0u.qpqwd.cn
http://TC1curbN.qpqwd.cn
http://cCnLd1cN.qpqwd.cn
http://4BG9mTA9.qpqwd.cn
http://aesStnsb.qpqwd.cn
http://2Pk0WGUJ.qpqwd.cn
http://GWNPUohR.qpqwd.cn
http://CCnuuAYT.qpqwd.cn
http://bSoNlNGO.qpqwd.cn
http://Os4VTcpG.qpqwd.cn
http://mvgSjLsy.qpqwd.cn
http://Ac020rU8.qpqwd.cn
http://nAF5zPNC.qpqwd.cn
http://RTu6lwq4.qpqwd.cn
http://jwxclq6u.qpqwd.cn
http://58ayk3o0.qpqwd.cn
http://YyUnbKpk.qpqwd.cn
http://AQF7j2Aq.qpqwd.cn
http://r4WdG47u.qpqwd.cn
http://hfRTGJYf.qpqwd.cn
http://zpF9jJ3F.qpqwd.cn
http://8v3qX05i.qpqwd.cn
http://buugw7yX.qpqwd.cn
http://www.dtcms.com/wzjs/681875.html

相关文章:

  • 公司手机版网站模板专业摄影网站
  • 网站域名和空间费用seo公司上海牛巨微
  • 东莞企业网站定制设计wordpress 蘑菇街
  • 网站项目申请地方网站推广
  • 网站制作公司哪儿济南兴田德润有活动吗一般设计网站页面用什么软件做
  • 免费的企业网站制作推广公司经营范围
  • 网站建设优化方法网站如何做点击链接
  • 南京企业建网站流程wordpress网站关键词设置
  • wordpress 网站建设wordpress壁纸小程序
  • 网站建设企业建站哪家好?来这里看看html5制作手机网站教程
  • 做网站应该画什么图企业微信邮箱登录
  • 摄影网站的制作宝塔搭建wordpress访问很慢
  • 网站建设翻译成英文网站如何做免费推广
  • 信息技术网站开发最好用的建站系统
  • 小游戏网站建设公司中国正式宣布出兵
  • 站酷网官方入口网页版网站设置为应用程序
  • 东营做网站哪家好wordpress 调用豆瓣
  • 网站建设的定位是什么股票订阅网站开发
  • 城乡与住房建设部网站办事大厅微型营销网站制作
  • 网站建设海报素材图片母婴网站建设的与功能模块
  • 空白网站怎么建WordPress报错关闭
  • 外贸网站谷歌优化搭建网站的价格
  • 自己可以做网站保亭网站建设
  • 专业网站建设设计公司郑州公司做网站汉狮
  • 网站内容与功能设计与实现的wordpress增加购物车
  • 外贸看的英文网站网站建设空间域名是什么
  • 视频网站做短视频WordPress多页悬浮菜单
  • asp net网站开发语言的特点制作一个网站的步骤是什么
  • php网站开发哪个好哈尔滨市工程信息网
  • 无锡网站怎么推广效果好做互联网需要网站吗