从头开始学C语言第二十二天——字符串函数

在使用字符串函数时,需要引用字符串库string.h

常用的字符串函数包括

strlen 求字符串长度函数

strcpy 字符串拷贝函数

strcat 字符串连接函数

strcmp 字符串比较函数

strlen

格式:strlen(字符数组)

功能:求字符串长度,输出字符串有效长度,不包括'\0'在内。如果在字符串中出现'\0',系统只会计算'\0'前面的字符数据。

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

int main()
{
    char a[]={'a','\0','b','c','\0','d'};
    int n;

    n = strlen (a);

    printf("%d %d",n,sizeof(a));

    return 0;
}

运行代码我们可以发现,n=1,而a=6。因此我们可以知道strlen和sizeof之间的区别,strlen是一个函数,用于计算字符数组中'\0'前面的字符数据;而sizeof是一个运算符,用于计算整个字符数组所占用的内存大小,包括'\0'。

\xhh 输出十六进制代表的符号;\ddd 输出八进制代表符号。下面的代码b字符数组中的字符结构是:\t,\v,\\,\c,\a。总共有5个数据,因此strlen(b)=5,sizeof(b)=6。(因为字符串数组是以'\0'结束,因此'\0'也被算做是占用了一个字节的内存)

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

int main()
{
    char a[]={'a','\0','b','c','\0','d'};
    char b[]="\t\v\\\c\a";
    char c[]="\x69\141";

    printf("%d %d",strlen(a),sizeof(a));
    puts("");
    printf("%s \n%d %d",b,strlen(b),sizeof(b));
    puts("");
    printf("%s \n%d %d",c,strlen(c),sizeof(c));


    return 0;
}

strcpy

格式:strcpy(字符串1,字符串2);

功能:将字符串2拷贝到字符串1中;

返回值:返回字符数组1的首地址。

注意:在进行拷贝时,字符数组1必须要足够大,至少能容纳字符数组2全部字符数据+1个字节的容量大小。因为拷贝操作会将'\0'一同拷贝。

#include<stdio.h>
#include<string.h>
#define N 20

int main()
{
    char a[N]="hello";
    char b[]="world";
    int i,n;

    printf("%s",a);
    puts("");

    strcpy(a,b);

    printf("%s\n%s",a,b);

    return 0;

}

我们也可以用学过的循环实现strcpy函数的功能。思路如下 

#include<stdio.h>
#include<string.h>
#define N 20

int main()
{
    char a[N]="hello";
    char b[]="world";
    int i,n;

    printf("%s",a);
    puts("");
    
    n = strlen(b);
    //strlen计算的是有效长度,不包括'\0'
    //但是strcpy函数会把'\0'一起拷贝进去
    //因此进行循环的时候在strlen得到
    //有效长度之后,再+1,保证将'\0'一并赋值才可以等同于strcpy。

    for(i=0;i<n+1;i++)
    {
        a[i] = b[i];
    }

    printf("%s\n%s",a,b);

    return 0;

}

strcat 

格式:strcat(字符数组1,字符数组2);

功能:把字符数组2连接到字符数组1后面

返回值:返回字符数组1的首地址

注意:strcat和strcpy一样,在连接时字符数组1必须足够大,在连接之前,两个数组都是以'\0'结束,但是在连接后,字符数组1的'\0'取消,在新的字符串最后加上'\0'。

#include<stdio.h>
#include<string.h>
#define N 20

int main()
{
    char a[N]={'h','e','l','l','o'};
    char b[]=" world!!";

    printf("%p\n%p",a,b);

    strcat(a,b);

    printf("\n%s",a);

    return 0;
}

strcmp

格式:strcmp(字符串1,字符串2);

功能:比较两个字符串

规则:逐个比较字符串每个字符对应的ASCII码,直到遇到不同的字符或者'\0'为止。

返回值:返回int型整数。如果字符串1>字符串2,返回正整数;如果字符串1<字符串2,返回负整数;如果字符串1=字符串2,返回0。

#include<stdio.h>
#include<string.h>
#define N 20

int main()
{
    char a[N]="hello";
    char b[]={"aello"};

    printf("%d",strcmp(a,b));

    return 0;
}

字符串拓展用法

strncpy(str1,str2,n)→n表示复制指定长度的字符串

stnrcat(str1,str2,n)→n表示附加指定长度的字符串

strncmp(str1,str2,n)→n表示比较指定长度的字符串

strcasecmp→忽略大小写比较字符串

#include<stdio.h>
#include<string.h>
#define N 20

int main()
{
    char a[N]="hello";
    char b[]={"heLLO"};

    //strncpy(a,b,2);//复制指定长度字符串
    //strncat(a,b,2);//附加指定长度字符串

    //printf("%s",a);

    //printf("%d",strncmp(a,b,2));//比较指定长度字符串
    //puts("");
    printf("%d",strcasecmp(a,b));//忽略大小写比较字符串大小

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

神阶平天牛魔王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值