关于字符串函数的实现
来源:互联网 发布:涉外域名纠纷案件 编辑:程序博客网 时间:2024/05/16 09:32
在很多面试及笔试中,都会有字符串的操作,有的甚至不允许使用C函数库中的函数,所使用函数都需要自己编写。那么就有必要针对字符串函数的实现,进行一个讲解。
首先:
将详细讲解在字符串函数实现中常用的语句
*str++
实际上是先使用*str,再str = str+1;这句话经常出现。比如计算字符串长度int StrLen(char *str){if (NULL == str)exit(0);int length = 0;while(*str++){length++;}return length;}假如我们不使用*str++这个语句,那么必然要引入i,
int StrLen(char *str){int length = 0,i;for (i = 0; *(str + i) != '\0'; i++){length++;;}return length;}
这样就造成了语句的冗余,尤其在后面的字符串比较及其他更复杂操作,*str++的方便性更能体现。
其次:
1.字符串长度函数
如上已经给出
2.字符串比较函数
3.字符串复制函数
4.字符串连接函数
对于这些函数我将会在稍后将完整程序列出。
头文件 StrSelf.h
#ifndef _STR_SELF_#define _STR_SELF_#include <stdlib.h>#include<stdio.h>int StrLen(char *str);int StrCmp(char *str1, char *str2);char* StrCpy(char *StrDst, char *StrSrc);char * StrCat(char *StrDst,char *StrSrc);#endif
函数实现文件StrSelf.cpp#include "StrSelf.h"int StrLen(char *str){if (NULL == str)exit(0);int length = 0;while(*str++){length++;}return length;}// 0 equal ,< 0 less,>0 larger int StrCmp(char *str1, char *str2){if (NULL == str1 || NULL == str2)exit(0);while(*str1 && *str2 &&(*str1 == *str2)){str1++;str2++;}return *str1 - *str2;}char* StrCpy(char *StrDst, char *StrSrc){if (NULL == StrDst || NULL == StrSrc)exit(0);char *address = StrDst;while(*StrSrc){*StrDst++ = *StrSrc++;}*StrDst = '\0';return address;}char * StrCat(char *StrDst,char *StrSrc){char * address = StrDst;while(*StrDst)StrDst++;while(*StrSrc){*StrDst++ = *StrSrc++;}*StrDst = '\0';return address;}
部分测试文件#include<stdio.h>#include "StrSelf.h"#define MaxNumDstStr 20int main(void){char StrDst[100] = "i love";char *StrSrc = "qin";printf("strsrc is %s\n",StrSrc);printf("strdst is %s\n",StrDst);printf("strdst is %s\n",StrCat(StrDst, StrSrc));return 0;}
- 关于字符串函数的实现
- 关于字符串的函数
- 几个常考的关于字符串的函数实现
- 关于字符串中函数的实现( C语言 )
- 字符串函数的实现
- 字符串函数的实现
- 字符串函数的实现
- 字符串函数的实现
- 字符串函数的实现
- 关于字符串截取的函数
- 关于字符串的几个函数
- 总结关于字符串的函数
- 关于字符串的copy函数
- 一些关于字符串的函数
- 字符串问题及关于字符串的函数
- 关于字符串反转的实现
- 字符串处理函数的实现
- 字符串类函数的实现
- Base64的 c实现
- C++ makefile写法标准格式简介
- JMS中的session和重发机制
- Windows API实现文件的查找
- Delphi TRzButtonEdit的用法
- 关于字符串函数的实现
- 创业者必读的21本书
- shell 内置命令shift
- 漫谈Java程序的性能优化
- vb6.0 控件适应窗口大小
- Function handling 函数 实例
- rails ajax jquery 应用
- 中文分词的一些研究记录[转]
- 发短信