1 串类型的定义
一、串和基本概念
串(String)是零个或多个字符组成的有限序列。一般记S=“a1a2a3…an”,其中S是串名,双引号括起来的字符序列是串值;ai(1≦i≦n)可以是字母、数字或其它字符;串中所包含的字符个数称为该串的长度。长度为零的串称为空串(Empty String),它不包含任何字符。
通常将仅由一个或多个空格组成的串称为空白串(Blank String)
注意:空串和空白串的不同,例如“ ”和“”分别表示长度为1的空白串和长度为0的空串。
串的基本操作
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef CString_h__
#define CString_h__
typedef struct _CString
{
char *p;
int reallength;
}Mystring;
//清空初始化
void InitString(Mystring *str);
//初始化字符串
void InitWithString(Mystring *str, char *SourceString);
//求长度
int StrLen(char *str);
//字符串链接
char *StrCat(char *DestStr, const char *SourStr);
//打印
void PrintString(Mystring *str);
//字符串拷贝
char *StrCpy(char *DestString, const char *SourString);
//字符串后添加字符
void AddCharWithBack(Mystring *str, const char ch);
//字符串后添加字符串
void AddStrWithBack(Mystring *str, char *newstr);
//执行命令
void RunSys(Mystring *str);
//查找字符
char *SearchFirstChar(char *str, char ch);
//找到第一个字符的地址
char *FindFirstChar(Mystring *str, char ch);
//删除找到的第一个字符
int DeleteFirstChar(Mystring *str, char ch);
//替换第一个找到的字符
void ReplaceFirstChar(Mystring *str, char ch, char newChar);
//查找字符串
char *StrStr(char *str, char *findstr);
//找到相同字符串的首地址
char *FindFirstString(Mystring *str, char *findstr);
//删除找到的字符串
int DeleteFirstString(Mystring *str, char *findstr);
//改变找到的字符串
void ReplaceFirstString(Mystring *str, char *oldstring, char *newstring);
//增加任意字符
void AddChar(Mystring *str, char ch, char *pos);
//增加任意字符串
void AddString(Mystring *str, char *addstr, char *pos);
#endif // CString_h__
以上函数的实现
#include "CString.h"
//初始化
void InitString(Mystring *str)
{
str->p = NULL;
str->reallength = 0;
}
//初始化字符串
void InitWithString(Mystring *str, char *SourceString)
{
int length = StrLen(SourceString);
str->reallength = length + 1;
str->p = (char *)malloc(sizeof(char) * str->reallength);
StrCpy(str->p, SourceString);
}
//求长度
int StrLen(char *str)
{
if (NULL == str)
{
return -1;
}
int length = 0;
while (*str != '\0')
{
length++;
str++;
}
return length;
}
//打印
void PrintString(Mystring *str)
{
printf("字符串是 : %s\n", str->p);
}
//字符串拷贝
char *StrCpy(char *DestString, const char *SourString)
{
if (NULL == DestString || NULL == SourString)
{
return NULL;
}
char *Tempstr = DestString;
while (*SourString != '\0')
{
*DestString++ = *SourString++;
}
*DestString = '\0';
return Tempstr;
}
//字符串后添加字符
void AddCharWithBack(Mystring *str, const char ch)
{
if (StrLen(str->p)+1 >= str->reallength)
{
str->p = realloc(str->p, str->reallength + 1);
str->reallength += 1;
str->p[str->reallength - 2] = ch;
str->p[str->reallength - 1] = '\0';
}
else
{
int nowlength = StrLen(str->p);
str->p[nowlength] = ch;
str->p[nowlength + 1] = '\0';
}
}
//字符串后添加字符串
void AddStrWithBack(Mystring *str, char *newstr)
{
int strLength = StrLen(str);
int newLength = StrLen(newstr);
if ((strLength + newLength + 1) >= str->reallength)
{
int needLength = (strLength + newLength + 1) - str->reallength;
str->p = (char *)realloc(str->p, str->reallength + needLength);
StrCat(str->p, newstr);
str->reallength += needLength;
}
else
{
StrCat(str->p, newstr);
}
}
//字符串链接
char *StrCat(char *DestStr, const char *SourStr)
{
if (NULL == DestStr || NULL == SourStr)
{
return NULL;
}
char *TempStr = DestStr;
while (*DestStr != '\0')
{
DestStr++;
}
while (*SourStr != '\0')
{
*DestStr = *SourStr;
DestStr++;
SourStr++;
}
*DestStr = '\0';
return TempStr;
}
//执行命令
void RunSys(Mystring *str)
{
system(str->p);
}
//返回找到的第一个字符的地址
char *SearchFirstChar(char *str, char ch)
{
if (NULL == str)
{
return NULL;
}
while (*str != '\0')
{
if (ch == *str)
{
return str;
}
str++;
}
return NULL;
}
//找到第一个字符的地址
char *FindFirstChar(Mystring *str, char ch)
{
char *p = SearchFirstChar(str->p, ch);
return p;
}
//删除找到的第一个字符
int DeleteFirstChar(Mystring *str, char ch)
{
char *p = SearchFirstChar(str->p, ch);
if (NULL == p)
{
return 0;
}
else
{
char *pnext = p + 1;
while (*pnext != '\0')
{
*p = *pnext;
p++;
pnext++;
}
*pnext = '\0';
return 1;
}
}
//替换第一个找到的字符
void ReplaceFirstChar(Mystring *str, char ch, char newChar)
{
char *TempStr = str->p;
while (*TempStr != '\0')
{
if (ch == *TempStr)
{
*TempStr = newChar;
}
TempStr++;
}
}
//查找字符串
char *StrStr(char *str, char *findstr)
{
for (int i = 0; str[i] != '\0'; ++i)
{
int flag = i;
int j = 0;
if (str[i] == findstr[j])
{
while (str[i++] == findstr[j++])
{
if (findstr[j] == '\0')
{
return &str[flag];
}
}
}
i = flag;
}
return NULL;
}
//找到相同字符串的首地址
char *FindFirstString(Mystring *str, char *findstr)
{
char *Result = StrStr(str->p, findstr);
if (NULL == Result)
{
return NULL;
}
return Result;
}
//删除找到的字符串
int DeleteFirstString(Mystring *str, char *findstr)
{
char *p = StrStr(str->p, findstr);
if (NULL == p)
{
return 0;
}
int length = StrLen(findstr);
char *pnext = p + length;
while (*pnext != '\0')
{
*p = *pnext;
p++;
pnext++;
}
*p = '\0';
return 1;
}
//增加任意字符
void AddChar(Mystring *str, char ch, char *pos)
{
if (NULL == str || NULL == pos)
{
return;
}
if (StrLen(str->p) + 1 >= str->reallength)//内存不够
{
//分配内存
str->p = (char *)realloc(str->p, str->reallength + 1);
str->reallength += 1;
//添加字符
int strlength = StrLen(str->p);
int movelength = StrLen(pos);
for (int i = strlength; i > (strlength - movelength); --i)
{
str->p[i] = str->p[i - 1];
}
str->p[strlength - movelength] = ch;
str->p[strlength + 1] = '\0';
}
else//内存够直接添加
{
int strlength = StrLen(str->p);
int movelength = StrLen(pos);
for (int i = strlength; i > (strlength - movelength); --i)
{
str->p[i] = str->p[i - 1];
}
str->p[strlength - movelength] = ch;
str->p[strlength + 1] = '\0';
}
}
//增加任意字符串
void AddString(Mystring *str, char *addstr, char *pos)
{
if (NULL == pos || NULL == str)
{
return;
}
int strlength = StrLen(str->p);
int addstrlength = StrLen(addstr);
if (strlength + addstrlength + 1 >= str->reallength)
{
int needlength = strlength + addstrlength - str->reallength + 1;
str->p = (char *)realloc(str->p, str->reallength + needlength);
str->reallength += needlength;
int nowlength = StrLen(str->p);
int movelength = StrLen(pos);
int insertlength = StrLen(addstr);
for (int i = nowlength; i > nowlength - movelength; i--)
{
str->p[i + insertlength] = str->p[i];
}
for (int j = 0; j < insertlength; j++)
{
str->p[nowlength - movelength] = addstr[j];
}
}
else
{
int nowlength = StrLen(str->p);
int movelength = StrLen(pos);
int insertlength = StrLen(addstr);
for (int i = nowlength; i > nowlength - movelength; i--)
{
str->p[nowlength - movelength] = str->p[i];
}
for (int j = 0; j < addstrlength; j++)
{
str->p[nowlength - movelength] = addstr[j];
}
}
}
//改变找到的字符串
void ReplaceFirstString(Mystring *str, char *oldstring, char *newstring)
{
char *pfind = FindFirstString(str, oldstring);
if (NULL != pfind)
{
DeleteFirstString(str, oldstring);
AddString(str, newstring, pfind);
}
}
函数测试
#include <stdio.h>
#include "CString.h"
int main(void)
{
Mystring str;
// InitWithString(&str, "calc");
// RunSys(&str);
InitWithString(&str, "Hello C++ ");
PrintString(&str);
AddCharWithBack(&str, '!');
PrintString(&str);
AddStrWithBack(&str, "My Name Is C++");
PrintString(&str);
// ReplaceFirstChar(&str, 'C', 'J');
// PrintString(&str);
// int deleteresult = DeleteFirstString(&str, "llo");
// if (0 == deleteresult)
// {
// printf("删除失败\n");
// }
// else
// {
// printf("删除成功\n");
// }
// PrintString(&str);
// printf("找到的第一次出现字符串地址是 %s\n", FindFirstString(&str, "llo"));
// printf("找到的第一个字符 C 的地址是:%x\n", FindFirstChar(&str, 'C'));
//
// int deletereslult = DeleteFirstChar(&str, 'C');
// if (0 == deletereslult)
// {
// printf("删除字符失败\n");
// }
// else
// {
// printf("删除字符成功\n");
// }
// PrintString(&str);
// AddChar(&str, 'F', "C++");
// PrintString(&str);
ReplaceFirstString(&str, "llo", "popopopo");
PrintString(&str);
return 0;
}