首页 > 代码库 > 自己实现的字符串基本操作
自己实现的字符串基本操作
#include "stdafx.h"#include <stdio.h>#include <stdlib.h>#include <string.h>#define TRUE 1#define FALSE 0#define BIG 1#define EQUAL 0#define SMALL -1unsigned int strlenth(char *s) /* 获取字符串长度 */{ unsigned int lenth = 0; if (!s) return 0; while (‘\0‘ != s[lenth]) { lenth++; } return lenth;}void strcopy(char **target, char *source) /* 字符串拷贝 */{ unsigned int i = 0; unsigned int length = 0; char *pTmp = *target; length = strlenth(source); while(‘\0‘ != source[i]) { pTmp[i] = source[i]; i++; } pTmp[i] = ‘\0‘;}int strcompare(char *s, char *t) /* 字符串比较,s>t,则返回1;s=t,则返回0;s<t,则返回-1 */{ unsigned int uiIndex = 0; unsigned int uiLenOfS = strlenth(s); unsigned int uiLenOfT = strlenth(t); if (0 != uiLenOfS && 0 == uiLenOfT) //如果s字符数不为0,而t字符数为0,则返回1 { return BIG; } else if (0 == uiLenOfS && 0 != uiLenOfT)//如果s字符数为0,而t字符数不为0,则返回-1 { return SMALL; } else if (0 == uiLenOfS && 0 == uiLenOfT)//如果s和t字符数都为0,则返回0。字符数为0的情况有空串或字符串指针为空 { return EQUAL; } else //s和t的字符数都不为0 { /* 同时遍历两个字符串,只要有一个扫描完,就退出 */ while (‘\0‘ != s[uiIndex] && ‘\0‘ != t[uiIndex]) { if (s[uiIndex] > t[uiIndex]) { return BIG; } else if (s[uiIndex] < t[uiIndex]) { return SMALL; } else { ; } uiIndex++; } /* 扫描结束后,哪个字符串没扫描完,则说明这个字符串大;如果都扫描完了,说明相等 */ if (‘\0‘ != s[uiIndex] && ‘\0‘ == t[uiIndex] ) { return BIG; } else if (‘\0‘ == s[uiIndex] && ‘\0‘ != t[uiIndex] ) { return SMALL; } else { return EQUAL; } }}void strcombine(char **s, char *t) /* 字符串连接,将字符串t接到s后面,x为连接后的新串 */{ unsigned int uiLen = 0; char *pTmp = *s; if (NULL == s || NULL == t) { return; } uiLen = strlenth(pTmp) + strlenth(t); memcpy(pTmp + strlenth(pTmp), t, strlenth(t));}void strcatch(char *s, unsigned int index, unsigned int lenth, char **t) /* 字符串截取,从第index个字符开始,截取lenth长度的字符串,并输出到字符串t */{ char *pTmp = *t; char *pSrc =http://www.mamicode.com/ s; if (NULL == s) { return; } if (index < 0 || lenth <= 0 || index + lenth >= strlenth(s)) { return; } while (index--) { pSrc++; } memcpy(pTmp, pSrc, lenth);}bool strsubstr(char *s, char *sub) /* 字符串子串查找,如果子串sub在s中存在,则返回1,否则返回0 */{ unsigned int uiIndexOfS = 0; unsigned int uiIndexOfSub = 0; unsigned int uiLenOfS = strlenth(s); unsigned int uiLenOfSub = strlenth(sub); if (NULL == s || NULL == sub) { return FALSE; } if (uiLenOfS < uiLenOfSub) //如果子串长度大于原串,则返回0 { return FALSE; } while (‘\0‘ != s[uiIndexOfS]) { if (s[uiIndexOfS] == sub[0]) { while (‘\0‘ != sub[uiIndexOfSub]) { if (s[uiIndexOfS] != sub[uiIndexOfSub]) { return FALSE; } uiIndexOfS++; uiIndexOfSub++; } if (‘\0‘ == sub[uiIndexOfSub]) { return TRUE; } } uiIndexOfS++; } if (‘\0‘ != sub[uiIndexOfSub]) { return FALSE; } else { return TRUE; }}int main(){ char str1[] = "Hello"; char str2[] = " World"; char str3[] = "Hello"; char *str4 = (char*)malloc(100); if (!str4) return 0; memset(str4, 0, 100); char *str5 = (char*)malloc(100); if (!str5) return 0; memset(str5, 0, 100); strcopy(&str5, str1); strcombine(&str5, str2); strcatch(str5, 0, 5, &str4); printf("str5 = %s\n", str5); printf("The length of str1 is %u\n", strlenth(str1)); printf("strcompare(str1, str2) = %d\n", strcompare(str1, str2)); printf("strcompare(str1, str3) = %d\n", strcompare(str1, str3)); printf("strsubstr(str5, str1) = %d\n", strsubstr(str5, str1)); printf("Call strcatch(str5, 0, 5, &str4), str4 = %s\n", str4);}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。