1.strlen函数
strlen函数的作用是计算给定字符串的长度,从内存的某个位置开始,遇到第一个'\0'结束。
使用样例:
int main()
{
const char *ar = "abcdef";
printf("%d\n", strlen(ar));
}
返回字符串"abcdef"的长度为6。
strlen函数的实现:
方法一:
size_t my_strlen(const char*string)
{
assert(string != NULL);//断言,对参数进行有效性检查
int count = 0;
while (*string != '\0')
{
count++;
string++;
}
return count;
}
方法二(通过递归):
size_t my_strlen(const char*string)
{
assert(string != NULL);
if (*string == '\0')
return 0;
return my_strlen(string + 1) + 1;
}
2.strcpy函数
strcpy,即string copy,字符串复制函数,把含有'\0'结束符的字符串复制到另一个空间地址,返回值类型为char *。
使用样例:
void main()
{
char str[20] = "Hello";//str一定要有足够的空间,要能容纳拷贝进来的数据
char *str1 = "World";
char *res=strcpy(str, str1);
printf("str=%s\n",res);
}
将str1拷贝到str中,结果为:
str一定要有足够的空间,要能容纳拷贝进来的数据,否则就会拷贝失败,如下图所示:
strcpy函数的实现:
char *my_strcpy(char *strDestination, const char *strSource)
{
assert(strDestination != NULL && strSource != NULL);//检查参数
char *pDest = strDestination; //保护参数
const char *pSrc = strSource;
while (*pSrc!='\0') //拷贝数据
{
*pDest = *pSrc;
pSrc++;
pDest++;
}
*pDest = '\0';//拷贝结束标记
return strDestination; //返回结束标记
}
3.strcat函数
strcat函数为字符串连接函数,它的功能是在一个字符串后面追加上一个字符串。
使用样例:
void main()
{
char str[20] = "Hello";
char *str1 = "World";
char *res1=strcat(str, str1);
printf("str=%s\n", res1);
}
将str1连接到str后面,结果为:
strcat函数的实现:
char *my_strcat(char *strDestination, const char *strSource)
{
assert(strDestination != NULL && strSource != NULL);//检查参数
char *pDest = strDestination; //保护参数
const char *pSrc = strSource;
while (*pDest != '\0')
pDest++;
while (*pSrc != '\0')
*pDest++ = *pSrc++;
*pDest = '\0';
return strDestination;
}
4.strcmp函数
strcmp,即string compare,是字符串比较函数,用于比较两个字符串并根据比较结果返回整数。
函数样例:
int main()
{
char *s1 = "hello";
char *s2 = "Hello";
int res3 = strcmp(s1, s2);
printf("%d\n", res3);
}
若s1=s2,返回0;
s1>s2,返回一个大于0的数;
s1<s2,返回一个小于0的数。
此样例中s1>s2,所以返回值为1。
strcmp函数实现:
方法1:
int my_strcmp(const char *string1, const char *string2)
{
assert(string1 != NULL && string2 != NULL);//检查参数
int res;
while (*string1 != '\0'|| *string2 != '\0')
{
if (res=(*string1 - *string2 )!= 0)
break;
string1++;
string2++;
}
if (res > 0)
return 1;
else if (res < 0)
return -1;
return res;
}
方法2:
int my_strcmp(const char *string1, const char *string2)
{
assert(string1 != NULL && string2 != NULL);//检查参数
while (*string1 != '\0'&& *string2 != '\0')
{
if (*string1 > *string2)
return 1;
else if (*string1 < *string2)
return -1;
else
{
string1++;
string2++;
}
}
if (*string1 != '\0')
return 1;
if (*string2 != '\0')
return -1;
return 0;
}
5.strncpy,strncat,strncmp函数补充介绍
strncpy,strncat,strncmp为长度受到限制的字符串函数。功能和stecpy,strcat,strcmp函数相同,只是控制了作用的字符串长度。
strncpy函数的实现:
char *my_strncpy(char *strDest, const char *strSource, size_t count)
{
assert(strDest != NULL&&strSource != NULL);
char *pDest = strDest;
const char *pSrc = strSource;
while (count > 0)
{
*pDest = *pSrc;
pDest++;
pSrc++;
count--;
}
return strDest;
}
strncat函数的实现:
char *my_strncat(char *strDest, const char *strSource, size_t count)
{
assert(strDest != NULL&&strSource != NULL);
char *pDest = strDest;
const char *pSrc = strSource;
while (*pDest != '\0')
pDest++;
while (count > 0)
{
*pDest = *pSrc;
pDest++;
pSrc++;
count--;
}
return strDest;
}
strncmp函数的实现:
int my_strncmp(const char *string1, const char *string2, size_t count)
{
assert(string1 != NULL&&string2 != NULL);
int res;
while (count > 0)
{
if ((res=(*string1-*string2))!=0)
break;
string1++;
string2++;
count--;
}
if (res > 0)
return 1;
else if (res < 0)
return -1;
else;
return res;
}