字符串函数

本文详细介绍了C语言中用于字符串操作的strlen、strcmp、strcpy、strcat等函数,包括它们的用途、实现原理及示例。通过示例代码展示了如何计算字符串长度、比较字符串、复制和连接字符串。同时,提到了字符串搜索函数strchr和strstr的使用,以及可能出现的安全问题和解决方法。
摘要由CSDN通过智能技术生成

在C语言中,有一些专门针对字符串做运算的函数,它们分别是strlen、strcmp、strcpy、strcat等等。它们的函数声明放在了头文件string.h中。

1 strlen函数

strlen函数的参数是一个字符串,返回值是字符串中除结尾‘\0’外的元素个数。例如:

#include<stdio.h>
#include<string.h>

void main(void)
{
    char *a="hello !";
    printf("strlen所计算出的字符串长度为%d\n",strlen(a));
}

代码运行结果为:

strlen所计算出的字符串长度为7

显然strlen在计算字符串长度时,并没有将结尾的‘\0’包含进去。这也是它与sizeof方法计算字符串长度的不同之处。例如:

#include<stdio.h>
#include<string.h>


void main(void)
{
    char *a="hello !";
    printf("strlen所计算出的字符串长度为%d\n",strlen(a));
    printf("sizeof所计算出的字符串长度为%d\n",sizeof(a)/sizeof(a[0]));
}

代码运行结果为:

strlen所计算出的字符串长度为7
sizeof所计算出的字符串长度为8

显然sizeof方法所计算出的字符串长度是包括结尾的'\0'在内的。

自己写一个mylen函数替代strlen可是如下所示:

#include<stdio.h>
#include<string.h>

int mylen(const char *s)
{
    int index=0;
    while (s[index]!='\0')
    {
        index++;
    }
    return index;
}
void main(void)
{
    char *a="hello !";
    printf("strlen所计算出的字符串长度为%d\n",strlen(a));
    printf("sizeof所计算出的字符串长度为%d\n",sizeof(a)/sizeof(a[0]));
    printf("mylen所计算出的字符串长度为%d\n",mylen(a));
}

代码运行结果为:

strlen所计算出的字符串长度为7
sizeof所计算出的字符串长度为8
mylen所计算出的字符串长度为7 

2 strcmp函数

该函数的原型为:

int strcamp(char *a,char*b)

函数参数是两个字符串a和b,返回值是整型变量(值只能为-1,1和0)。函数的作用是,将a、b字符的自左向右每一位进行比较,直到遇到不相等的那一位结束,如果a中的那一位比b中对应位的asc码更大,则返回1,否则返回-1。如若直到比较到最后一位(包含'\0'),都没有遇到不相等的位,则认为两个字符串相等,并返回0。调试程序如下:

#include<stdio.h>
#include<string.h>

void main(void)
{
    char *a="abc";
    char *b="abc";
    char *c="aad";
    char *d="abc ";
    printf("%d\n",strcmp(a,b)); 
    printf("%d\n",strcmp(a,c)); 
    printf("%d\n",strcmp(a,d)); 
}

运行结果为:

0
1
-1

自己写一个mycmp替代strcmp的功能,代码可以如下所示:

#include<stdio.h>
#include<string.h>

int mycmp(const char* a,const char* b);

void main(void)
{
    char *a="abc";
    char *b="abc";
    char *c="aad";
    char *d="abc ";
    printf("%d\n",strcmp(a,b)); 
    printf("%d\n",mycmp(a,b));
    printf("%d\n",strcmp(a,c)); 
    printf("%d\n",mycmp(a,c)); 
    printf("%d\n",strcmp(a,d));
    printf("%d\n",mycmp(a,d));  
}

int mycmp(const char* a,const char* b)
{
    int result= 0;
    while(((*a)!='\0')||((*b)!='\0'))
    {
        if (*a>*b)
        {
            result=1;
            break;
        }
        else if (*a<*b)
        {
            result=-1;
            break;
        }
        a++;
        b++;
    }
    return result;
}

运行结果为:

0
0
1
1
-1
-1

3 strcpy函数

该函数的原型为:

char * strcpy(char restrict *dst,char restrict *src)

函数的参数是两个字符串(指针),返回值是从dst开始的字符串。其作用是,将src处开始的字符串拷贝到dst处开始。restrict 关键字是用来限制源字符串地址与目标字符串地址不能有重叠的部分。

注意:由于一般来说,我们不知道源字符串的长度,故常与malloc配合使用,以申请合适大小的目标字符串空间。并且,源字符串的‘\0’不会被拷贝到目标字符串,所以我们通常需要在目标字符串末尾手动加上‘\0’,申请空间时也要记得。调试代码为:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

void main(void)
{
    char *src="hello";
    char *dst=(char*)malloc(sizeof(src)/sizeof(src[0]));
    printf("dst的内容是:%s",strcpy(dst,src));
}

运行结果为:

dst的内容是:hello

4 strcat函数

该函数的原型为:

char * strcat(char restrict *a,char restrict *b)

该函数的参数为两个字符串a和b,返回值为a与b组合形成的一个新字符串。该函数的作用是将b字符串拷贝到a的后面。restrict 关键字是用来限制a字符串地址与b字符串地址不能有重叠的部分。

同样,该函数一般也会与malloc配合使用。调试程序如下所示:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

void main(void)
{
    char *a="hello ";
    char *b="world!";
    printf("%s",strcat(a,b));
}

运行代码,发现报错。这是因为,strcat是将b字符串拷贝到a字符串后面,但是代码中用char *a="hello " 的方式定义字符串a时,只留了存放7个元素的空间,没有空间再去存放b字符串的元素,所以,身体里插头函数运行失败。我们调整代码:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

void main(void)
{
    char a[20]="hello ";
    char *b="world!";
    printf("%s",strcat(a,b));
}

代码运行结果为:

hello world!

代码运行成功,这是因为我们在定义a字符串时为其留了很大的内存空间(20个元素),b中元素可以拷贝到a的后面。那么我们如果在a后面拷贝一个元素很多,所需空间很大的字符串,那么strcat函数就会出现类似内存不够用的安全性问题,这种问题在strcpy中也存在。

为了解决这种问题,可以使用安全版的字符串函数:

int strcmp(char *a,char *b,int n),该函数对a,b字符串的前n个元素进行比较。

char * strcpy(char *a,char *b,int n),该函数将b字符串的前n个元素拷贝到a中。

char * strcat(char *a,char *b,int n),该函数将b字符串的前n个元素拷贝到a后。

5 字符串搜索函数

5.1 strchr/strrchr函数

函数原型为:

char * strchr(const char *a,int n)

该函数的参数是一个字符串a和一个字符n,该函数的返回值是字符串a中字符n的地址指针。该函数的作用是在a字符串中自左向右寻找字符n,如果搜索到一个,立即停止,不在搜索,则返回a字符串中搜索到的b的指针,如果没有搜索到,返回null(或者0地址值)。char * strrchr(const char *a,int n)函数与char * strchr(const char *a,int n)作用相同,唯一不同在于char * strrchr(const char *a,int n)是自右向左搜索字符n。调试程序如下:

#include<stdio.h>
#include<string.h>

void main(void)
{
    char *a="hello";
    char  b='l';
    char  c='i';
    printf("%p\n",strchr(a,b));
    printf("%p\n",strrchr(a,b));
    printf("%p\n",strchr(a,c));
    printf("%s\n",strchr(a,b));
    printf("%s\n",strrchr(a,b));
}

代码运行结果为:

0000000000404002
0000000000404003
0000000000000000
llo
lo

5.2 strstr/strcasestr函数

函数原型为:

char * strstr(const char *a,const char *b)

该函数的作用是在a字符串中搜索b字符串。该函数的参数是字符串a和b。返回值为指针型变量。该函数用在在a字符串中自左向右搜索b字符串,一旦搜索到,立即停止搜索,即使后面还有该字符串,也不搜索,并返回搜索到的字符串在a中的第一个元素的指针。调试程序为:

#include<stdio.h>
#include<string.h>

void main(void)
{
    char *a="abcdefghdeflmn";
    char *b="def";
    char *c="DEF";
    printf("%p\n",strstr(a,b));
    printf("%s\n",strstr(a,b));
}

运行结果为:

0000000000404003
defghdeflmn

由结果可知,返回值是a字符串中第一个‘d’的指针。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值