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

网站栏目做树形结构图浙江网络科技有限公司

网站栏目做树形结构图,浙江网络科技有限公司,网站有哪些区别是什么,高校校园网网站内容如何建设各位CSDN的uu们你们好呀,今天小雅兰来给大家介绍一个全新的知识点,就是字符函数和字符串函数啦,其实其中有些函数我之前已经学习过了,比如strlen、strcpy;也有一些之前不是很熟悉的函数,比如strstr、strtok…

各位CSDN的uu们你们好呀,今天小雅兰来给大家介绍一个全新的知识点,就是字符函数和字符串函数啦,其实其中有些函数我之前已经学习过了,比如strlen、strcpy;也有一些之前不是很熟悉的函数,比如strstr、strtok、strerror等等。话不多说啦,现在,让我们进入字符函数和字符串函数的世界吧


求字符串长度

        strlen

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

        strcpy

        strcat

        strcmp

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

        strncpy

        strncat

        strncmp


C语言中对字符和字符串的处理很是频繁,但是C语言本身是没有字符串类型的,字符串通常放在

常量字符串中或者字符数组中。

字符串常量适用于那些对它不做修改的字符串函数.


strlen

 

 size_t strlen ( const char * str );

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

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
int main()
{char arr[] = "abc\0def";int len = strlen(arr);printf("%d\n", len);return 0;
}

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
int main()
{char arr[] = "abcdef";int len = strlen(arr);printf("%d\n", len);return 0;
}

 

 abcdef后面隐藏了一个\0

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

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
int main()
{char arr[3] = { 'a','b','c'};int len = strlen(arr);printf("%d\n", len);return 0;
}

如果字符串不以\0结束,那么,结果就是一个随机值 

注意函数的返回值为size_t,是无符号的

#define _CRT_SECURE_NO_WARNINGS 1
#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

三种方式:

        1.计数器的方式

        2.递归的方式

        3.指针-指针的方式

函数递归+青蛙跳台阶——“C”_认真学习的小雅兰.的博客-CSDN博客

指针——“C”_认真学习的小雅兰.的博客-CSDN博客

1.计数器的方式

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<assert.h>
int my_strlen(const char* str)
{//计数器的方式int count = 0;assert(str != NULL);while (*str != '\0'){str++;count++;}return count;
}
int main()
{char arr[] = "abcdef";int len = my_strlen(arr);printf("%d\n", len);return 0;
}

2.递归的方式

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
//不能创建临时变量,求字符串的长度
int my_strlen(const char * str)
{if(*str=='\0')return 0;elsereturn 1 + my_strlen(str+1);
}int main()
{char arr[] = "abcdef";int len = my_strlen(arr);printf("%d\n", len);return 0;
}

 3.指针-指针的方式

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
//指针-指针的方式
int my_strlen(char * s)
{char * p = s;while( * p != '\0')p++;return p-s;
}int main()
{char arr[] = "abcdef";int len = my_strlen(arr);printf("%d\n", len);return 0;
}

 

strcpy 

 

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

#include<stdio.h>
#include<string.h>
int main()
{char arr1[3] = { 'a','b','c' };char arr2[20] = "xxxxxx";strcpy(arr2, arr1);printf("%s\n", arr2);return 0;
}

这样程序直接崩溃了

会将源字符串中的 '\0' 拷贝到目标空间。

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

#include<stdio.h>
#include<string.h>
int main()
{//错误的示范char arr1[20] = "abcdefghi";char arr2[3] = "";strcpy(arr2, arr1);printf("%s\n", arr2);return 0;
}

目标空间必须可变。

#include<stdio.h>
#include<string.h>
int main()
{//错误的示范char* p = "abcdefghi";char arr2[20] = "hehe";strcpy(p, arr2);printf("%s\n", p);return 0;
}

 

模拟实现strcpy 

#include<stdio.h>
#include<string.h>
//1.参数顺序
//2.函数的功能,停止条件
//3.assert
//4.const修饰指针
//5.函数返回值
//6.题目出自《高质量C/C++编程》书籍最后的试题部分
//返回的是目标空间的起始地址
#include<assert.h>
char * my_strcpy(char * dest, const char* src)
{char * ret = dest;assert(dest!=NULL);assert(src != NULL);while ((*dest++ = *src++)){;}return ret;
}
int main()
{char arr1[] = "hehe";char arr2[20] = { 0 };my_strcpy(arr2, arr1);printf("%s\n", arr2);return 0;
}

 

strcat

 

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

#include<stdio.h>
#include<string.h>
int main()
{char arr1[20] = "hello ";char arr2[] = "world";//追加strcat(arr1, arr2);printf("%s\n", arr1);return 0;
}

 

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

#include<stdio.h>
#include<string.h>
int main()
{char arr1[20] = "hello \0xxxxxxxxxxxx";char arr2[] = "world";//追加strcat(arr1, arr2);printf("%s\n", arr1);return 0;
}

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

目标空间必须可修改。

 模拟实现strcat

#include<stdio.h>
#include<assert.h>
char* my_strcat(char* dest, const char* src)
{char* ret = dest;assert(dest != NULL);assert(src != NULL);//找目标空间的\0while (*dest!='\0'){dest++;}//拷贝while ((*dest++ = *src++)){;}return ret;
}
int main()
{char arr1[20] = "hello ";char arr2[] = "world";//追加my_strcat(arr1, arr2);printf("%s\n", arr1);return 0;
}

 

 绝对不能自己给自己追加!!!

#include<stdio.h>
#include<assert.h>
char* my_strcat(char* dest, const char* src)
{char* ret = dest;assert(dest != NULL);assert(src != NULL);//找目标空间的\0while (*dest != '\0'){dest++;}//拷贝while ((*dest++ = *src++)){;}return ret;
}
int main()
{char arr1[20] = "bit";//追加my_strcat(arr1, arr1);printf("%s\n", arr1);return 0;
}

 

 strcmp

 

“abcdef"=="bbcdef",这里比较的是两个字符串首字符的地址,而并不是字符串的内容 

比较两个字符串内容的时候,不能使用==,应该使用strcmp

                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的数字
#include<stdio.h>
#include<string.h>
#include<assert.h>
int main()
{char arr1[] = "abcdef";char arr2[] = "bbcdef";int ret = strcmp(arr1, arr2);printf("%d\n", ret);return 0;
}

 

模拟实现strcmp 

#include<stdio.h>
#include<string.h>
#include<assert.h>
int my_strcmp(const char* str1, const char* str2)
{assert(str1 != NULL);assert(str2 != NULL);while (*str1 == *str2){if (*str1 == '\0'){return 0;}str1++;str2++;}if (*str1 > *str2){return 1;}else{return -1;}
}int main()
{char arr1[] = "abcdef";char arr2[] = "bbcdef";int ret = my_strcmp(arr1, arr2);printf("%d\n", ret);return 0;
}

另一种写法:

#include<stdio.h>
#include<string.h>
#include<assert.h>
int my_strcmp(const char* str1, const char* str2)
{assert(str1 != NULL);assert(str2 != NULL);while (*str1 == *str2){if (*str1 == '\0'){return 0;}str1++;str2++;}return *str1 - *str2;
}
int main()
{char arr1[] = "abcdef";char arr2[] = "bbcdef";int ret = my_strcmp(arr1, arr2);printf("%d\n", ret);return 0;
}

 


strncpy

 

         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>
#include<assert.h>
int main()
{char arr1[] = "abcdef";char arr2[5] = { 0 };strncpy(arr2, arr1, 3);printf("%s\n", arr2);return 0;
}

 

 strncat

 

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

  • 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.
#include<stdio.h>
#include<string.h>
#include<assert.h>
int main()
{char arr1[20] = "hello \0xxxxxxxx";char arr2[] = "world";strncat(arr1, arr2, 3);printf("%s\n", arr1);return 0;
}

strncmp

 

 

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

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

#include<stdio.h>
#include<string.h>
#include<assert.h>
int main()
{char arr1[] = "abcdef";char arr2[] = "abcq";int ret = strncmp(arr1, arr2, 4);printf("%d\n", ret);return 0;
}

 

 


好啦,小雅兰今天的内容就到这里啦,还要继续加油呀!!!

 

http://www.hkea.cn/news/32319/

相关文章:

  • 适合友情链接的网站排名函数
  • 开发公司岗位设置广州seo招聘网
  • 国内web设计网站宣传推广
  • 深圳高端网站定制公司小时seo
  • wordpress主菜单下拉箭头怎么设置台州seo排名优化
  • 网站系统管理员模块关键词查找工具
  • 望江县建设局网站外贸seo推广招聘
  • 微信网站上传图片手机怎么制作网站
  • 简单做网站需要学什么搜索引擎有哪些网站
  • 网站备案信息加到哪里如何进行网站推广
  • 昭通网站制作aso优化技巧
  • 制作网站时怎样做滚动字幕新网站多久会被百度收录
  • 余姚物流做网站微信指数是搜索量吗
  • 怎样做网站轮播今日国内重大新闻事件
  • 想给大学做网站百度网盘搜索神器
  • jsp网站开发论文官方app下载安装
  • 关于机场建设的网站今日疫情最新情况
  • 网站域名注册服务商google浏览器官方
  • 通过网站开发工具怎么改自动跳网站百度指数有哪些功能
  • 可以发锚文本的网站百度搜索官方网站
  • 东莞网站建设企慕简述如何优化网站的方法
  • 可以做网站的公司seo外包
  • 自己怎么做网站视频赚钱5g网络优化培训
  • 数据库修改网站管理员密码seo网站有优化培训吗
  • 福田做商城网站建设找哪家公司好抖音怎么运营和引流
  • 厘米售卡站怎么做网站禁止搜索引擎收录的方法
  • 网站首页滚动图片怎么做谷歌搜索关键词排名
  • 嵩县网站开发友情链接获取的途径有哪些
  • 国家企业信息公示网(广东)海南快速seo排名优化
  • 高端网站设计 上海徐州seo排名公司