#include <stdlib.h>
unsigned int strlenth(char *s) /* 获取字符串长度 */
{
unsigned int lenth = 0;
if(s!=NULL)
{
while(*s!='\0')/* 代码在这里实现 */
{
s++;
lenth++;
}
}
return lenth;
}
void strcopy(char **target, char *source) /* 字符串拷贝 */
{
/* 代码在这里实现 */
*target=(char*)malloc(strlenth(source)+1);
char*dest=*target;
while((*dest++=*source++)!='\0');
}
int strcompare(char *s, char *t) /* 字符串比较,s>t,则返回1;s=t,则返回0;s<t,则返回-1 */
{
/* 代码在这里实现 */
if(s!=NULL&&t!=NULL)
{
while(*s!='\0'&&*t!='\0')
{
if(*s>*t)
return 1;
else if(*s<*t)
return -1;
else
{
s++;
t++;
}
}
if(*s=='\0'&&*t!='\0')
return -1;
else if(*s!='\0'&&*t=='\0')
return 1;
else
return 0;
}
return 0;
}
void strcombine(char **x, char *s, char *t) /* 字符串连接,将字符串t接到s后面,x为连接后的新串 */
{
/* 代码在这里实现 */
if(s==NULL||t==NULL)
return;
int len1=strlenth(s);
int len2=strlenth(t);
*x=(char*)malloc(len1+len2+1);
char*dest=*x;
//*x=*dest;
while(len1--)
*dest++=*s++;
while(len2--)
*dest++=*t++;
*dest='\0';
}
void strcatch(char *s, unsigned int index, unsigned int lenth, char **t) /* 字符串截取,从第index个字符开始,截取lenth长度的字符串,并输出到字符串t */
{
/* 代码在这里实现 */
int ll=strlenth(s);
if(s==NULL||index<0||(index>=ll)||lenth<=0)
return;
if(index+lenth<=ll)
{
*t=(char*)malloc(lenth+1);
char *dest=*t;
while(lenth--)
{
*dest++=*(s+index);
s++;
}
*dest='\0';
}
}
bool strsubstr(char *s, char *sub) /* 字符串子串查找,如果子串sub在s中存在,则返回1,否则返回0 */
{
bool result = 0;
/* 代码在这里实现 */
if(s!=NULL&&sub!=NULL)
{
char*q=s;
int len=strlenth(sub);
while(*s++!='\0')
{
char*p=sub;
int i;
for( i=0;i<len;i++)
{
if((*q!='\0')&&(*q==*p))
{
q++;
p++;
}
else
{
q=s;
break;}
}
if(i==len)
{
result=true;break;
}
}
}
return result;
}
字符串基本操作
最新推荐文章于 2022-12-04 00:22:51 发布