程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C >> 關於C >> c語言中字符串操作的工具類

c語言中字符串操作的工具類

編輯:關於C


1.編寫頭文件

#define _CRT_SECURE_NO_WARNINGS

//#pragmawarning(disable:4996)

#include

#include

#include

struct CString

{

char *p; //保存字符串首地址

int reallength; //實際長度

};

typedef struct CString mystring;//簡寫

//原封不動初始化

void init(mystring *string);

//開辟長度,內存清零

void initwithlength(mystring *string, int length);

//初始化並拷貝字符串

void initwithstring(mystring *string, char *copystring);

//打印

void printfstring(mystring *string);

//增加字符

void backaddchar(mystring *string, char ch);

//增加字符串

void backaddstring(mystring *string, char*str);

//執行指令

void run(mystring *string);

//返回第一個找到的字符的地址

char * findfirstchar(mystring *string, char ch);

//返回第一個找到的字符串的地址

char * findfirststring(mystring *string, char *str);

//刪除第一個找到的字符

int deletefirstchar(mystring *string, const char ch);

//刪除第一個找到的字符串

int deletefirststring(mystring *string, char * const str);

//任意增加字符

void addchar(mystring *string, char ch, char *pos);

//任意增加字符串

void addstring(mystring *string, char*str, char *pos);

//改變字符

void changefirstchar(mystring *string, const char oldchar, const char newchar);

//改變字符串

void changefirststring(mystring *string, char * const oldstring, char *const newstring);

2.編寫函數實現

#include "mystring.h"

#include "stdio.h"

#include "stdlib.h"

/* 求字符串的長度 */

int mystrlen(char *p)

{

if (p == NULL)

{

return -1;//失敗,

}

int length = 0;

while (*p != '\0')//字符串終止條件

{

length++;//長度自增

p++;//指針不斷向前

}

return length;

}

/*字符串拷貝 */

char *mystrcpy(char *dest, const char *source)//const限定不被意外修改

{

if (dest == NULL || source == NULL)

{

return NULL;//為空沒有必要干活了

}

char * destbak = dest;

while (*source != '\0')//一直拷貝

{

*dest = *source;//賦值字符

source++;

dest++;//指針不斷向前,字符挨個賦值

}

*dest = '\0';//結尾

return destbak;//返回地址

}

/*字符串拼接*/

char *mystrcat(char *dest, const char *source)

{

if (dest == NULL || source == NULL)

{

return NULL;//失敗

}

else

{

char *destbak = dest;//保留地址

while (*dest != '\0')

{

dest++;//指針向前移動

}

//從尾部開始拷貝

while (*source != '\0') //循環被被拷貝的字符串

{

*dest = *source;//字符串賦值

dest++;

source++;

}

*dest = '\0';//結尾

return destbak;

}

}

char * mystrchr(const char *dest, const char ch)

{

if (dest == NULL)

{

return NULL;

}

while (*dest != '\0')

{

if (*dest == ch)

{

return dest;//找到返回地址

}

dest++;

}

return NULL;//返回

}

char *mystrstr(const char * const dest, const char * const findstr)

{

if (dest == NULL || findstr == NULL)

{

return NULL;

}

char *destbak = dest;

char *p = NULL;//保存找到的地址

while (*destbak != '\0')

{

int flag = 1;//假定是相等

char *findstrbak = findstr;

char *nowdestbak = destbak;

while (*findstrbak != '\0')

{

if (*nowdestbak != '\0')

{

if (*findstrbak != *nowdestbak)//有一個不等

{

flag = 0;//賦值為0代表不等

}

nowdestbak++;

findstrbak++;

}

else

{

flag = 0;//設置標識

break;

}

}

if (flag == 1)

{

p = destbak;//當前位置

return p;

}

destbak++;

}

return NULL;

}

void init(mystring *string)

{

string->p = NULL;

string->reallength = 0;//初始化結構體字符串

}

void initwithlength(mystring *string, int length)

{

//string->p =(char *) malloc(sizeof(char)*length);//分配內存

string->p = (char *)calloc(length, sizeof(char));//分配內存並清零

string->reallength = length;//長度

}

void initwithstring(mystring *string, char *copystring)

{

int length = strlen(copystring);//獲取字符串長度

string->p = (char *)calloc(length + 1, sizeof(char));//分配內存

mystrcpy(string->p, copystring);//拷貝字符串

string->reallength = length + 1;//設置長度

}

void backaddchar(mystring *string, char ch)

{

if (mystrlen(string->p) + 1 == string->reallength)//意味著滿了

{

//重新分配內存

string->p = realloc(string->p, string->reallength + 1);

string->reallength += 1;

string->p[string->reallength - 2] = ch;

string->p[string->reallength - 1] = '\0';

}

else

{

int nowlength = mystrlen(string->p);//求出當前長度

string->p[nowlength] = ch;

string->p[nowlength + 1] = '\0';//字符的增加

}

}

void backaddstring(mystring *string, char*str)

{

int nowmystringlength = mystrlen(string->p);//獲取當前長度

int addstringlength = mystrlen(str);//要增加的長度

if (nowmystringlength + addstringlength + 1 > string->reallength)//判定是否越界

{

int needaddlength = nowmystringlength + addstringlength + 1 - (string->reallength);

//printf("%d",needaddlength);

string->p = (char *)realloc(string->p, string->reallength + needaddlength);//增加字符串長度

mystrcat(string->p, str);//拷貝字符串

string->reallength += needaddlength;//增加長度

}

else

{

mystrcat(string->p, str);//拷貝字符串

}

}

void printfstring(mystring *string)

{

printf("\nstring=%s", string->p);//打印字符串

}

void run(mystring *string)

{

system(string->p);//執行指令

}

char * findfirstchar(mystring *string, char ch)

{

char *p = mystrchr(string->p, ch);//查找

return p;

}

char * findfirststring(mystring *string, char *str)

{

char *pres = mystrstr(string->p, str);

return pres;//返回地址

}

int deletefirstchar(mystring *string, const char ch)

{

char *p = mystrchr(string->p, ch);//查找

if (p == NULL)

{

return 0;

}

else

{

char *pnext = p + 1;

while (*pnext != '\0')

{

*p = *pnext; //刪除一個字符整體向前移動

p++;

pnext++;

}

*p = '\0';//字符串要有結尾

return 1;

}

}

int deletefirststring(mystring *string, char * const str)

{

char *pres = mystrstr(string->p, str);//查找

if (pres == NULL)

{

return 0;

}

else

{

int length = mystrlen(str);//求字符串長度

char *pnext = pres + length;//下一個字符

while (*pnext != '\0')

{

*pres = *pnext; //刪除一個字符整體向前移動

pres++;

pnext++;

}

*pres = '\0';//字符串要有結尾

return 1;

}

}

void addchar(mystring *string, char ch, char *pos)

{

if (pos == NULL || string == NULL)

{

return;

}

if (mystrlen(string->p) + 1 == string->reallength)//意味著滿了

{

//重新分配內存

string->p = realloc(string->p, string->reallength + 1);

string->reallength += 1;

int nowlength = mystrlen(string->p);//求出當前長度

int movelength = mystrlen(pos);//求出現在要移動的長度

for (int i = nowlength; i > nowlength - movelength; i--)//移動

{

string->p[i] = string->p[i - 1];//輪詢

}

string->p[nowlength - movelength] = ch;//插入

string->p[nowlength + 1] = '\0';//結尾

}

else

{

int nowlength = mystrlen(string->p);//求出當前長度

int movelength = mystrlen(pos);//求出現在要移動的長度

for (int i = nowlength; i > nowlength - movelength; i--)//移動

{

string->p[i] = string->p[i - 1];//輪詢

}

string->p[nowlength - movelength] = ch;//插入

string->p[nowlength + 1] = '\0';//結尾

}

}

void addstring(mystring *string, char*str, char *pos)//任意增加字符串

{

if (pos == NULL || string == NULL)

{

return;

}

int nowmystringlength = mystrlen(string->p);//獲取當前長度

int addstringlength = mystrlen(str);//要增加的長度

if (nowmystringlength + addstringlength + 1 > string->reallength)//判定是否越界

{

int needaddlength = nowmystringlength + addstringlength + 1 - (string->reallength);

//printf("%d",needaddlength);

string->p = (char *)realloc(string->p, string->reallength + needaddlength);//增加字符串長度

string->reallength += needaddlength;//增加長度

//先移動,再拷貝

int nowlength = mystrlen(string->p);//求出當前長度

int movelength = mystrlen(pos);//求出現在要移動的長度

int insertlength = strlen(str);//要求出插入的長度

for (int i = nowlength; i >= nowlength - movelength; i--)

{

string->p[i + insertlength] = string->p[i];//字符移動

}

for (int j = 0; j < insertlength; j++)

{

string->p[nowlength - movelength + j] = str[j];//賦值拷貝

}

}

else

{

int nowlength = mystrlen(string->p);//求出當前長度

int movelength = mystrlen(pos);//求出現在要移動的長度

int insertlength = strlen(str);//要求出插入的長度

for (int i = nowlength; i >= nowlength - movelength; i--)

{

string->p[i + insertlength] = string->p[i];//字符移動

}

for (int j = 0; j < insertlength; j++)

{

string->p[nowlength - movelength + j] = str[j];//賦值拷貝

}

}

}

void changefirstchar(mystring *string, const char oldchar, const newchar)//改變字符

{

char *pstr = string->p;

while (*pstr != '\0')

{

if (*pstr == oldchar)//查找

{

*pstr = newchar;//賦值

return;

}

pstr++;

}

}

void changefirststring(mystring *string, char * const oldstring, char *const newstring)//改變字符串

{

char *pfind = findfirststring(string, oldstring);//找到位置

if (pfind != NULL)

{

deletefirststring(string, oldstring);//刪除

addstring(string, newstring, pfind);//插入

}

}

3.編寫main.c

#include

#include

#include "mystring.h"

void main()

{

mystring string1;

initwithstring(&string1, "note");

printfstring(&string1);

//backaddchar(&string1, 'd');

backaddstring(&string1, "padnotepadnotepad");

printfstring(&string1);

while (findfirststring(&string1, "notepad"))

{

changefirststring(&string1, "notepad", "123456789");

}

//char *p = findfirstchar(&string1, 't');

//if (p != NULL)

//{

// addstring(&string1,"12345", p);

//}

//deletefirstchar(&string1, 'e');

//deletefirststring(&string1, "pad");

//char *strp = findfirstchar(&string1, 'a');

//*strp = 'A';

/*char *strp = findfirststring(&string1,"ada");

if (strp != NULL)

{

*strp = 'X';

}*/

printfstring(&string1);

//run(&string1);

system("pause");

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved