C字符串操作函数的实现详细解析
作者:jingxian 时间:2022-10-26 16:28:37
1. strlen(),计算字符串长度
int strlen(const char string)
{
int i=0;
while(string[i]) i++;
return i;
}
2. strcpy(), 字符串拷贝.
char *strcpy(char *destination, const char *source)
{
while(*destinaton++=*source++);
return (destination-1);
}
3. strcat(), 字符串的连接.
char *strcat(char *target,const char *source)
{
char *original=target;
while(*target) target++; // Find the end of the string
while(*target++=*source++);
return(original);
}
4. streql(), 判断两个字符串是否相等.
int streql(char *str1,char *str2)
{
while((*str1==*str2)&&(*str1))
{
str1++;
str2++;
}
return((*str1==NULL)&&(*str2==NULL));
}
5. strchr(), 在字符串中查找某个字符.
char *strchr(const char *string,int letter)
{
while((*string!=letter)&(*string))
string++;
return (string);
}
6. chrcnt(), 计算某个字符在字符串中出现的次数.
int chrcnt(const char *string,int letter)
{
int count=0;
while(*string)
if(*string==letter)count++;
return count;
}
7. strcmp(), 判断两个字符串是否相等.
int strcmp(const char *str1,const char *str2)
{
while((*str1==*str2)&&(*str1))
{
str1++;
str2++;
}
if((*str1==*str2)&&(!*str1)) //Same strings
return o;
else if((*str1)&&(!*str2)) //Same but str1 longer
return -1;
else if((*str2)&&(!*str1)) //Same but str2 longer
else
return((*str1>*str2)?-1:1);
}
标签:字符串操作函数
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Springboot创建子父工程过程图解
2022-09-20 06:06:26
![](https://img.aspxhome.com/file/2023/6/77496_0s.png)
基于私钥加密公钥解密的RSA算法C#实现方法
2022-12-01 07:52:37
Java 全方位讲解面向对象特点与使用
2023-07-21 18:34:06
![](https://img.aspxhome.com/file/2023/4/68704_0s.png)
解析java中super的用法分析
2021-11-17 19:39:52
c#删除指定文件夹中今天之前的文件
2022-02-26 22:57:27
java字符串抉择
2023-08-08 14:20:05
![](https://img.aspxhome.com/file/2023/7/58077_0s.png)
Android实现京东首页效果
2023-10-16 23:10:10
![](https://img.aspxhome.com/file/2023/0/120140_0s.jpg)
WPF+SkiaSharp实现自绘拖曳小球
2023-03-25 01:31:14
![](https://img.aspxhome.com/file/2023/6/91286_0s.gif)
Spring Boot非Web项目运行的方法
2021-07-23 13:05:32
webuploader 实现图片批量上传功能附实例代码
2022-12-03 09:13:34
![](https://img.aspxhome.com/file/2023/8/67148_0s.png)
Java 数据结构与算法系列精讲之KMP算法
2023-05-06 14:55:55
![](https://img.aspxhome.com/file/2023/7/64907_0s.jpg)
C#方法中参数ref和out详解
2021-07-19 12:59:32
![](https://img.aspxhome.com/file/2023/7/106387_0s.png)
Java:泛型知识知多少
2023-11-24 23:08:44
![](https://img.aspxhome.com/file/2023/8/60188_0s.jpg)
Java实战在线选课系统的实现流程
2022-12-19 10:34:30
![](https://img.aspxhome.com/file/2023/4/86724_0s.png)
C#可用于登录验证码的四位随机数生成方法
2021-08-01 00:31:01
在Maven下代理服务器设定的方式
2023-10-15 02:17:13
![](https://img.aspxhome.com/file/2023/9/58569_0s.png)
浅谈Spring Boot 整合ActiveMQ的过程
2022-03-22 05:20:27
Spring Boot 中application.yml与bootstrap.yml的区别
2023-07-09 12:30:56
解决FeignClient发送post请求异常的问题
2022-08-08 15:55:58
java中String的一些方法深入解析
2023-11-25 21:48:56
![](https://img.aspxhome.com/file/2023/2/60332_0s.png)