当前位置:网站首页>Character Functions and String Functions
Character Functions and String Functions
2022-07-31 12:06:00 【Learn Java's Winter Melon】
目录
2、strcpy() strcat() strcmp()函数
3、strncpy() strncat() strncmp()Function to learn to use
1.2、islower() isupper() isalpha()函数
前言
Practice using the string evaluation function,And simulate some string functions
一、常见字符串函数
1、strlen()
2、strcpy() strcat() strcmp()长度不受限制的字符串函数
3、strncpy() strbcat() strncmp()长度受限制的字符串函数,比2Function safety in
4、strstr()
5、strtok()
1、strlen()求字符串长度
1.1、strlen()
意义:求字符串长度
size_t strlen( const char* str)
strlen()一'\0'作为结束标志,函数返回'\0'之前出现的字符个数
返回值类型是size_t表示unsigned int(无符号整形)
1>、使用计数器
代码如下(示例):
size_t my_strlen(char* str)
{
assert(str); //断言,若str=NULL则会报错
size_t cnt = 0;
while (*str != '\0')
{
cnt++;
str++;
}
return cnt;
}
2>、指针-指针
代码如下(示例):
size_t my_strlen(char* str)
{
assert(str);
char* start = str;
while (*str != '\0')
{
str++;
}
return str-start;
}
3>、使用递归
代码如下(示例):
size_t my_strlen(char* arr)
{
assert(arr);
if (*arr != '\0')
return 1 + my_strlen(arr + 1);
else
return 0;
}
2、strcpy() strcat() strcmp()函数
2.1、strcpy()
意义:拷贝字符串
char* strcpy(char* dest,const char* sorc)
拷贝时将sorc(source)中看不到的'\0',must be copied togetherdes中,否则出错
dest(目标destination)空间必须足够大,Make sure to store it
1>、简单思路
代码如下(示例):
void my_strcpy(char* dest,const char* sorc)
{
assert(dest);
assert(sorc);
while (*sorc!='\0')
{
*dest++ = *sorc++; //最后的'\0'Not in the loop
}
*dest = *sorc;
}
2>、优化后
代码如下(示例):
char* my_strcpy(char* dest,const char* sorc) //char*,返回一个函数指针,is the address of this function
{
assert(dest);
assert(sorc);
char* start = dest;
while (*dest++ = *sorc++) //当*sorc='\0'时,for assignment to occur,但不进入while内部
{ //赋值后再判断
;
}
return start; //The function pointer return value is char*,返回dest字符串首地址
}
2.2、strcat()
意义:追加字符串
char* strcat(char* dest,const char* sorc)
sorc字符串必须以'\0'结束,destThe string space must be large enough
不能自己给自己追加如my_strcat(arr1,arr1),因为'\0'被覆盖,Unable to judge the end
1>、优化后
代码如下(示例):
char* my_strcat(char* dest, const char* sorc)
{
assert(dest && sorc);
char* start = dest;
while (*dest != '\0') //1、找到dest的'\0'的位置用socr覆盖
{
dest++;
}
while (*dest++ = *sorc++)//2、拷贝sorc字符串
{
;
}
return start; //函数指针返回char*
}
2.3、strcmp()
意义:比较两个字符串的大小
int strcmp(const char*str1,const char* str2)
abz>abc abc<abcd abc==abc
规则:1、One-to-one comparisonASCII码值,当*str1>*str2,str1字符串就大,2、当遇到'\0'compared to other characters,All appear first'\0'The string is small
1>、简单思路
代码如下(示例):
int my_strcmp(const char* str1, const char* str2)
{
assert(str1 && str2);
while (*str1 == *str2 && *str1!='\0' &&*str2!='\0') //After the cycle is out'\0'
{ //Or after the loop is the character'0'和另一个字符比较('\0'than other charactersASCII码值小)
str1++;
str2++;
}
if (*str1 == *str2 && *str1 == '\0')
{
return 0;
}
else
{
if (*str1 > *str2)
{
return 1;
}
else
{
return -1;
}
}
}
2>、优化后
代码如下(示例):
int my_strcmp(const char* str1, const char* str2)
{
assert(str1 && str2);
while (*str1 == *str2 && *str1!='\0' &&*str2!='\0') //After the cycle is out'\0'
{ //Or after the loop is the character'0'和另一个字符比较('\0'than other charactersASCII码值小)
str1++;
str2++;
}
return *str1 - *str2; //The return value of greater than or equal to less is implemented here
}
3、strncpy() strncat() strncmp()Function to learn to use
和2、The implementation in is almost the same,就是多了个参数(个数限制)
3.1、strncpy()
意义:实现sorc字符串前size_t(unsigned int)character copy
char* strncpy(char* dest,const char* sorc,size_count)
3.2、strncat()
意义:实现sorc字符串前size_characters are appended
char* strncat(char* dest,const char* sorc,size_t count)
3.3、strncmp()
意义:实现sorc字符串前size_t个字符和dest字符串比较
int strncmp(const char *dest,const char *sorc,size_t count)
4、strstr()查找子串
4.1、strstr()
意义:查找子串
char* strstr(char* str1,const char* str2)
规则:string from motherstr1中查找子串str2,若找到,就Returns the first address of the position of the substring in the parent string,If you can't find it返回NULL
1>、代码一
Find it the first time:abcde 和 bcd,第n次找到:abcccdef 和 ccd 或 cde
代码如下(示例):
char* my_strstr(char* str1, const char* str2)
{
assert(str1 && str2);
char* s1 = str1;
char* s2 = str2;
char* start = s1; //When the first character is equal,start记录最开始的位置
while (*s1 != '\0')
{
while (*s1 != *s2 && *s1 != '\0')
{
s1++;
}
start = s1; //Record the first character of each equality,Then determine whether the following characters are equal
while (*s1 == *s2)
{
s1++;
s2++;
}
if (*s2 == '\0')
{
return start;
}
else
{
s1 = start + 1;//如果startStarted out of compliance,找start下一位
s2 = str2;
}
}
return NULL;
}
2>、代码二
代码如下(示例):
char* my_strstr(char* str1, const char* str2)
{
assert(str1 && str2);
char* s1 = str1;
char* s2 = str2;
char* start = s1; //When the first character is equal,start记录最开始的位置
while (*start)
{
s1 = start;
s2 = str2;
while (*s1 == *s2 && *s1 != '\0' && *s2 != '\0')
{
s1++;
s2++;
}
if (*s2 == '\0')
{
return start;
}
start++;
}
return NULL;
}
5、strtok()切割字符串
5.1、strtok()
意义:切割字符串
char* strtok(char* str,char*sep)
1、sep是一个字符串,定义了用作分隔符的字符集合
2、stris the cut string
规则:
一、1、第一次调用时strtok(str,esp),*str!=NULLfind firststr中第一个分隔符,将它改为'\0',然后返回头指针.2、因为会改变str字符串内容,所以需要先将strMake a copy to use
二、第二次调用时strtok(NULL,esp),The second call will remember to change after the first call'\0'的位置,*str==NULL时,will skip the previous one that was modified to'\0'的位置,Find the next delimiter
You can also print this way:
结果展示:
二、字符函数介绍
1、判断字符函数
为假返回0,Returns NOT for true0
1.1、iscntrl()
1>、控制字符
在ASCII码中,第0~31号及第127号(共33个)是控制字符或通讯专用字符,如控制符:LF(换行)、CR(回车)、FF(换页)、DEL(删除)、BS(退格)、BEL(振铃)等
2>、iscntrl()Print control charactersASCII码值
1.2、islower() isupper() isalpha()函数
判断字符小写、大写,或字母
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <ctype.h>
int cnt_a(char* str, int* cnt_up, int* cnt_alp)
{
char* start = str;
int cnt_low = 0;
//小写
while(*str!='\0')
{
if (isalpha(*str) != 0)
{
cnt_low++;
}
str++;
}
//大写
str = start;
while (*str != '\0')
{
if (isupper(*str) != 0)
{
(*cnt_up)++;
}
str++;
}
//字母
str = start;
while (*str != '\0')
{
if (isalpha(*str) != 0)
{
(*cnt_alp)++;
}
str++;
}
return cnt_low;
}
int main()
{
int cnt_upper = 0;
int cnt_alpha = 0;
int* up = &cnt_upper;
int* alp = &cnt_alpha;
char a[] = "My heart is hard";
printf("小写%d\n大写%d\n字母%d\n", cnt_a(a,up,alp), *up,*alp);
return 0;
}
2、字符转换函数
2.1、tolower() toupper()
tolower() -->把小写字母转换成大写字母,Non-lowercase letters are unchanged
toupper() -->corresponds to the above
总结
Content for string functions,The most important thing is to learn to draw,确定1、有几种情况2、指针指向哪里3、To simulate the implementation should borrow parameters、返回值的设置
边栏推荐
- WebGL给Unity传递参数问题1: Cannot read properties of undefined (reading ‘SendMessage‘)
- busybox之reboot命令流程分析
- Initial JDBC programming
- 瑞吉外卖项目:新增菜品与菜品分页查询
- CWE4.8 -- 2022年危害最大的25种软件安全问题
- A40i/T3 uboot启动时对PMU部分初始化
- Service discovery of kubernetes
- IDEA configure method annotation automatic parameters
- 关于==和equals的区别和联系,面试这么回答就可以
- 最全phpmyadmin漏洞汇总
猜你喜欢
chroot命令
学习爬虫之Scrapy框架学习(1)---Scrapy框架初学习及豆瓣top250电影信息获取的实战!
502 bad gateway causes and solutions
ESP8266-Arduino编程实例-HDC1008温度湿度传感器驱动
MySQL面试八股文(2022最新整理)
Exploring Plain Vision Transformer Backbones for Object Detection 论文阅读笔记
Is the working process of the belt you know the story - actionreducerstore
想吃菌子,当然是自己上山找了
Different lower_case_table_names settings for server (‘1‘) and data dictionary (‘0‘) 解决方案
In PLC communication error or timeout or download the prompt solution of the model
随机推荐
三层架构service、dao、controller层
Wearing detection and action recognition of protective gear based on pose estimation
R语言:文本(字符串)处理与正则表达式
A40i/T3 uboot启动时对PMU部分初始化
栈和队列的基本概念
生信周刊第38期
JS列表数据通过递归实现树形结构
In Excel using ODBC consumer SAP ABAP CDS view
基于稳态视觉诱发电位和注意力脑电的混合脑机接口系统
Different lower_case_table_names settings for server (‘1‘) and data dictionary (‘0‘) 解决方案
After class, watching the documentation and walking back to the lab, I picked up the forgotten SQL operators again
Summary of several defragmentation schemes for MySQL (to solve the problem of not releasing space after deleting a large amount of data)
dosbox基础使用[通俗易懂]
deeplab implements its own remote sensing geological segmentation dataset
PAT考试总结(考试心得)
Android studio connects to MySQL and completes simple login and registration functions
Caused by: 类找不到: org.apache.flink.table.planner.delegation.ParserFactory或者ExecutorFactory
jmeter性能测试步骤入门(性能测试工具jmeter)
【核心概念】图像分类和目标检测中的正负样本划分以及架构理解
MySQL index usage and optimization