实现自己的mystring.h
把常用的字符串处理函数写了一遍,没有考虑地址重叠。
/**************************************************/
char* mystrcpy( char *destin, const char *source )
{
if( destin == NULL || source == NULL )
{
return NULL;
}
char *addr = destin;
while( ( *destin++ = *source++ ) != '\0' );
return addr;
}
/**************************************************/
int mystrlen( const char *source )
{
if( source == NULL )
{
return 0;
}
int count = 0;
while( *source++ != '\0' )
{
count++;
}
return count;
}
/**************************************************/
char* mystrncpy( char *destin,const char *source, int num )
{
if( destin == NULL || source == NULL )
{
return NULL;
}
int limit = mystrlen( source );
char *addr = destin;
if( num > limit )
{
return NULL;
}
else
{
while( num-- )
{
*destin++ = *source++;
}
*destin = '\0';
return addr;
}
}
/**************************************************/
char* mystrcat( char *destin, const char *source )
{
if( destin == NULL || source == NULL )
{
return NULL;
}
char *addr = destin;
while( *destin++ != '\0' );
destin--;
while( ( *destin++ = *source++ ) != '\0' );
return addr;
}
/**************************************************/
char* mystrchr( char *source, char ch )
{
if( source == NULL )
{
return NULL;
}
while( *source != '\0' && ( *source++ != ch ) );
return *source == '\0' ? NULL:--source;
}
/**************************************************/
int mystrcmp( const char *destin, const char *source )
{
if( destin == NULL || source == NULL )
{
return destin - source;
}
while( *destin && *source && ( *destin++ == *source++ ) );
return *( destin -1 ) != *( source -1 ) ? \
*( destin-1 ) - *( source-1 ) : *destin - *source;
}
/**************************************************/
int mystrncmp( const char *destin, const char *source, int num )
{
if( destin == NULL || source == NULL || num <= 0 )
{
return destin - source;
}
while( num-- > 0 && *destin && *source && ( *destin++ == *source++ ) );
return *( destin-1 ) != *( source-1 ) || \
( num == 0 && *destin && *source ) ? \
*( destin-1 ) - *( source-1 ) : *destin - *source;
//考虑当进行最后一次比较时,如果字符串有一个为结束符,那么实际没有进行++
}
/**************************************************/
char* mystrstr( const char *source, const char *destin )
{
if( destin == NULL || source == NULL || \
*destin == '\0' || *source == '\0' )
{
return NULL;
}
char *position = (char *)source;
char *head = (char *)destin;
while( *source )
{
position = (char *)source;
destin = head;
while( *destin && *source && *destin++ == *source++ );
if( *destin == '\0' && *( destin-1 ) == *( source-1 ) )
{
return position;
}
if( *source == '\0' )
{
return NULL;
}
source = position + 1 ;
}
}
实现自己的mystring.h相关推荐
- 7-26晚上实现mystring
mystring.h #ifndef _MYSTRING_H_ #define _MYSTRING_H_#include <iostream> using namespace std; c ...
- C/C++面试题—实现MyString类
题目介绍 C++岗位面试题:实现自定义MyString类,实现拷贝构造函数和 [].=.+.== .!=.<< .>> 运算符重载函数. 自定义MyString估计是C++岗位 ...
- Mystring类实现运算符重载
Mystring.h #ifndef MYSTRING_H #define MYSTRING_H #include<iostream> using namespace std;class ...
- C++知识点42——下标运算符[]的重载及string类的实现
一.下标运算符的重载 1.概念 如果一个类表示容器,那么要重载下标运算符[],下标运算符必须是成员函数.下表访问运算符通常要有一个const版本和一个非const版本.如果不定义const版本,那么c ...
- 头文件包含【预处理】(58)
包含的意义 包含的方式 方式<> 方式" " 多文件编程 多文件编程意义 多文件编程的前提 多文件包含实例 定义头文件 谁用谁包含 自包含 避免头文件重复包含 包含的意 ...
- C++中的STRING数据 重载 = 和数组下标
Mstring_Test.c主文件 #include <iostream> #include "Mystring.h"using namespace std;int m ...
- C++文件头,命名空间,new和delete,内联函数,引用,函数重载,构造函数和析构函数,深拷贝和浅拷贝,explict,this指针
目 录 1 开始学习C++............................................................................... ...
- c语言中字符串操作的工具类
1.编写头文件 #define _CRT_SECURE_NO_WARNINGS //#pragmawarning(disable:4996) #include <stdio.h> # ...
- 怎么说呢。留个纪念,关于字符串的重载
等号重载的时候,一要记得先释放旧的内存!!!!!!!!!总忘记 MyString.h #pragma once #include<iostream> using namespace std ...
最新文章
- Flink从入门到精通100篇(八)-美团点评是如何在 Flink平台建立 实时数仓的?
- opencv python matplotlib.pyplot.hist() 如何绘制灰度直方图,如何根据灰度直方图确定最优二值化值
- QT判断操作系统版本
- MySQL从原理到实践,一篇从头到尾讲清楚
- 语言条件语序心得_教师心得:提高34岁幼儿口语表达能力的策略
- 网易北京:全员核酸检测为阴性 园区环境检测为阴性
- Flutter基础—开发环境与入门
- 常见视频编码比较大全 常见视频解码技术资料1
- 在linux上使用labelImg制作LMDB数据集——备忘TX2上运行
- 每日算法系列【LeetCode 470】用 Rand7() 实现 Rand10()
- 力扣-1534. 统计好三元组
- 奇妙的数字 小明发现了一个奇妙的数字。它的平方和立方正好把0~9的10个数字每个用且只用了一次。你能猜出这个数字是多少吗?
- gabor matlab pudn,matlab-Face-recognition 基于Gabor特征提取和人工智能的人脸检测系统 271万源代码下载- www.pudn.com...
- 大学什么专业学matlab,我选自动化专业,该专业在大学里学些什么课程?
- CSS3技巧:利用css3径向渐变做一张优惠券
- 我感觉被骗了,微信内测 “大小号” 功能,同一手机号可注册两个微信
- 什么是sp,怎么运作,他们是怎么发财的
- 轻量级程序编辑器的选择:EmEditor、Editplus等---Web开发系列之工具篇
- 计算机网络:CSMA/CD 与 CSMA/CA
- nginx做代理访问慢,优化方案