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

濮阳网站建设陈帅如何开发软件app

濮阳网站建设陈帅,如何开发软件app,0基础wordpress,网络公司经营范围写电子商务各位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.hrbkazy.com/news/13264.html

相关文章:

  • 长春阿凡达网站建设网络营销到底是个啥
  • 广西最优秀的品牌网站建设公司北京seo优化服务
  • wordpress做的网站效果6百度网站关键词排名助手
  • 滨海做网站哪家最好百度知道下载安装
  • 房天下怎样快速做网站外链发布工具下载
  • 怎么给领导做网站分析网络兼职平台
  • 女生做网站推广营销型网站建设服务
  • 企业网站的开发公司今天最火的新闻头条
  • 泉州seo优化搜索引擎优化方案
  • 电商网站多少钱在百度上做广告推广要多少钱
  • 重庆营销型网站建设沛宣网络宣传推广
  • 央美老师做的家具网站整合营销是什么
  • wordpress淘宝联盟模板快速排名优化公司
  • 自己做的网站 怎么在网上销售西安网站设计
  • 不用服务器做视频网站吗网站建设的基本流程
  • 专业做网文的网站怎么做自己的网站
  • 湘潭网络推广重庆seo网站系统
  • 网站建设从零开始教程全自动推广引流软件免费
  • 哪个网站教做西餐新闻发布平台
  • 怎么做网站编辑百度平台商家客服电话
  • 黑龙江省建设网证书查询长沙优化科技有限公司正规吗
  • 网站开发设计步骤360推广
  • 网站被挂黑链怎么办如何自己创建一个网站
  • 外贸网站运营怎么做搜索引擎排名中国
  • 网站品牌建设功能全网营销推广怎么做
  • 朗姿青春日记 网站谁做的视频推广平台
  • 山东建设企业网站百度推广注册
  • 网站建设建网站2023新闻摘抄十条
  • 网站公安备案一般什么可以做刷关键词的平台
  • 合肥仿站定制模板建站关键词优化排名软件