C字符串总结+字符串库实现(增,改,删,查):

《一》,字符指针&字符数组

两者形式:

字符指针:char *p;

字符数组:char str[100];

两者区别:

字符指针p是变量;

字符数组str是常量;

访问元素方式:

字符指针:*(p+i) || p[i],    访问第i个元素;

字符数组:*(str+i) || str[i]   访问第i个元素;

利用 sizeof() 求大小:

字符指针: sizeof(p) = 4,       sizeof(p)求的是指针p所占的空间,恒为4字节;

字符数组: sizeof(str) = 100, sizeof(str)求的是数组str的真实空间,100;

字符串函数strlen()说明:

strlen()仅仅求出字符串中第一个'\0'之前的字符个数,

printf("%s",str)只输出第一个'\0'之前的字符个数;

函数原型:

int strlen(char *p)

{

int count=0;

if(p==NULL)

return count;

while(*p++ != '\0')

count++;

return count;

}

简单实例:

 #define  _CRT_SECURE_NO_WARNINGS //关闭安全检查
#include<stdio.h>
#include<stdlib.h>
#include<string.h> //for语句
int mystrlen(char* str)
{
int i = ;
for (char*p = str; *p != '\0'; p++)
{
i++;
}
return i;//实现字符串统计
} //go语句
int mystrlenA(char* str)
{
char *p = str;
int i = ;
AAA: if (*p!='\0')
{
i++;
p++;
goto AAA;
}
return i;
} //递归实现
int mystrlenB(char *str)
{
if (*str=='\0')
{
return ;
}
else
{
return + mystrlenB(++str);
} } void main()
{
char str[] = "china is good";
char *p = str;
char *q = malloc(sizeof(char)*);
q = "hello,i am q";
printf("str:%s",str);
printf("\nsizeof(str):%d", sizeof(str));
printf("\nstrlen(str):%d", strlen(str));
printf("\n\np:%s", p);
printf("\nsizeof(p):%d", sizeof(p));
printf("\nstrlen(p):%d", strlen(p));
printf("\n\nq:%s", q);
printf("\nsizeof(q):%d", sizeof(q));
printf("\nstrlen(q):%d", strlen(q)); printf("\n%d", mystrlen(str));//从开头第一个字符到\0字符
printf("\n%d", mystrlenA(str));
printf("\n%d", mystrlenB(str)); getchar();
}

运行结果:

C字符串总结+字符串库实现(增,改,删,查):

《二》

字符串库实现:

String.h

 #define  _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h> //字符串封装,需要库函数
//不需要库函数
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 newchar);//改变字符
void changefirststring(mystring *string, char * const oldstring, char *const newstring);//改变字符串

String.c

 #include"字符串.h"

 int mystrlen(char *p)
{
if (p == NULL)
{
return -;//失败,
}
int length = ;
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 = ;//假定是相等
char *findstrbak = findstr;
char *nowdestbak = destbak;
while (*findstrbak != '\0')
{
if (*nowdestbak != '\0')
{ if (*findstrbak != *nowdestbak)//有一个不等
{
flag = ;//赋值为0代表不等
}
nowdestbak++;
findstrbak++;
}
else
{
flag = ;//设置标识
break;
} }
if (flag == )
{
p = destbak;//当前位置
return p;
} destbak++;
} return NULL; } void init(mystring *string)
{
string->p = NULL;
string->reallength = ;//初始化结构体字符串
}
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 + , sizeof(char));//分配内存
mystrcpy(string->p, copystring);//拷贝字符串
string->reallength = length + ;//设置长度 }
void backaddchar(mystring *string,char ch)
{ if (mystrlen(string->p)+==string->reallength)//意味着满了
{
//重新分配内存
string->p = realloc(string->p, string->reallength + );
string->reallength += ;
string->p[string->reallength - ] = ch;
string->p[string->reallength - ] = '\0'; }
else
{
int nowlength = mystrlen(string->p);//求出当前长度
string->p[nowlength] = ch;
string->p[nowlength + ] = '\0';//字符的增加 } }
void backaddstring(mystring *string, char*str)
{
int nowmystringlength = mystrlen(string->p);//获取当前长度
int addstringlength = mystrlen(str);//要增加的长度
if (nowmystringlength + addstringlength+ >string->reallength)//判定是否越界
{
int needaddlength = nowmystringlength + addstringlength + - (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 ; }
else
{ char *pnext = p + ;
while (*pnext != '\0')
{
*p = *pnext; //删除一个字符整体向前移动
p++;
pnext++;
}
*p = '\0';//字符串要有结尾
return ;
} }
int deletefirststring(mystring *string, char * const str)
{
char *pres = mystrstr(string->p, str);//查找
if (pres == NULL)
{
return ; }
else
{
int length = mystrlen(str);//求字符串长度
char *pnext = pres + length;//下一个字符
while (*pnext != '\0')
{
*pres = *pnext; //删除一个字符整体向前移动
pres++;
pnext++; }
*pres = '\0';//字符串要有结尾
return ;
} } void addchar(mystring *string, char ch, char *pos)
{
if (pos == NULL || string ==NULL)
{
return;
}
if(mystrlen(string->p) + == string->reallength)//意味着满了
{
//重新分配内存
string->p = realloc(string->p, string->reallength + );
string->reallength += ; int nowlength = mystrlen(string->p);//求出当前长度
int movelength = mystrlen(pos);//求出现在要移动的长度
for (int i = nowlength; i > nowlength - movelength; i--)//移动
{
string->p[i] = string->p[i - ];//轮询
}
string->p[nowlength - movelength] = ch;//插入 string->p[nowlength + ] = '\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 - ];//轮询
}
string->p[nowlength - movelength]=ch;//插入
string->p[nowlength + ] = '\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 + >string->reallength)//判定是否越界
{
int needaddlength = nowmystringlength + addstringlength + - (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 = ; 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 = ; 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);//插入
} }

特别说明:《C Primer Plus》第五版 第10章有数组,指针,函数的详细讨论,11章详细介绍字符串·相关操作;

上一篇:iOS开发中的4种数据持久化方式【二、数据库 SQLite3、Core Data 的运用】


下一篇:android开发之路12(android四大组件&Fragment&AsyncTask类)