把常用的字符串处理函数写了一遍,没有考虑地址重叠。

/**************************************************/
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相关推荐

  1. 7-26晚上实现mystring

    mystring.h #ifndef _MYSTRING_H_ #define _MYSTRING_H_#include <iostream> using namespace std; c ...

  2. C/C++面试题—实现MyString类

    题目介绍 C++岗位面试题:实现自定义MyString类,实现拷贝构造函数和 [].=.+.== .!=.<< .>> 运算符重载函数. 自定义MyString估计是C++岗位 ...

  3. Mystring类实现运算符重载

    Mystring.h #ifndef MYSTRING_H #define MYSTRING_H #include<iostream> using namespace std;class ...

  4. C++知识点42——下标运算符[]的重载及string类的实现

    一.下标运算符的重载 1.概念 如果一个类表示容器,那么要重载下标运算符[],下标运算符必须是成员函数.下表访问运算符通常要有一个const版本和一个非const版本.如果不定义const版本,那么c ...

  5. 头文件包含【预处理】(58)

    包含的意义 包含的方式 方式<> 方式" " 多文件编程 多文件编程意义 多文件编程的前提 多文件包含实例 定义头文件 谁用谁包含 自包含 避免头文件重复包含 包含的意 ...

  6. C++中的STRING数据 重载 = 和数组下标

    Mstring_Test.c主文件 #include <iostream> #include "Mystring.h"using namespace std;int m ...

  7. C++文件头,命名空间,new和delete,内联函数,引用,函数重载,构造函数和析构函数,深拷贝和浅拷贝,explict,this指针

     目 录 1      开始学习C++............................................................................... ...

  8. c语言中字符串操作的工具类

     1.编写头文件 #define _CRT_SECURE_NO_WARNINGS //#pragmawarning(disable:4996) #include <stdio.h> # ...

  9. 怎么说呢。留个纪念,关于字符串的重载

    等号重载的时候,一要记得先释放旧的内存!!!!!!!!!总忘记 MyString.h #pragma once #include<iostream> using namespace std ...

最新文章

  1. Flink从入门到精通100篇(八)-美团点评是如何在 Flink平台建立 实时数仓的?
  2. opencv python matplotlib.pyplot.hist() 如何绘制灰度直方图,如何根据灰度直方图确定最优二值化值
  3. QT判断操作系统版本
  4. MySQL从原理到实践,一篇从头到尾讲清楚
  5. 语言条件语序心得_教师心得:提高34岁幼儿口语表达能力的策略
  6. 网易北京:全员核酸检测为阴性 园区环境检测为阴性
  7. Flutter基础—开发环境与入门
  8. 常见视频编码比较大全 常见视频解码技术资料1
  9. 在linux上使用labelImg制作LMDB数据集——备忘TX2上运行
  10. 每日算法系列【LeetCode 470】用 Rand7() 实现 Rand10()
  11. 力扣-1534. 统计好三元组
  12. 奇妙的数字 小明发现了一个奇妙的数字。它的平方和立方正好把0~9的10个数字每个用且只用了一次。你能猜出这个数字是多少吗?
  13. gabor matlab pudn,matlab-Face-recognition 基于Gabor特征提取和人工智能的人脸检测系统 271万源代码下载- www.pudn.com...
  14. 大学什么专业学matlab,我选自动化专业,该专业在大学里学些什么课程?
  15. CSS3技巧:利用css3径向渐变做一张优惠券
  16. 我感觉被骗了,微信内测 “大小号” 功能,同一手机号可注册两个微信
  17. 什么是sp,怎么运作,他们是怎么发财的
  18. 轻量级程序编辑器的选择:EmEditor、Editplus等---Web开发系列之工具篇
  19. 计算机网络:CSMA/CD 与 CSMA/CA
  20. nginx做代理访问慢,优化方案

热门文章

  1. 判断推理——定义判断
  2. Echarts中的legend文字过长挡住图表文字解决
  3. 今日!云洲智造直播间开讲啦
  4. 七、jsp基础(二)- 内置对象
  5. Mac、iPad 之间拖拽即可移动文件、iOS 15 来了,这届 WWDC21 精彩内容尽在这里!...
  6. STM32控制两路直流电机_2
  7. [Usaco2014 Mar]Sabotage
  8. 爱心图案 -《跟小海龟学Python》案例代码
  9. 科诚Godex RT730x 打印机驱动
  10. 我国超级计算机历代,神威计算机图片_神威太湖之光_神威计祘机