第七章 字符串处理函数

三、字符串追加函数

头文件:#include <string.h>

函数定义:char *strcat(char *dest, const char *src);

函数功能:

strcat,函数追加 src,字符串到 dest,指向的字符串的后面。追加的时候会追加”\0’

注意:保证 dest 指向的内存空间足够大。

#include <stdio.h>
#include<string.h>
int main()
{
    char s1[100]="hello world";
    char s2[100]="abcdefg";
    strcat(s1,s2);
    printf("s1=%s\n",s1);//s1=hello worldabcdefg
    return 0;
}

char *strncat(char *dest, const char *src, size_t n);

追加 src,指向的字符串的前 n个字符,到 dest.指向的字符串的后面。注意如果 n 大于 src ,的字符个数,则只将 src,字符串追加到 dest,指向的字符串的后面追加的时候会追加’\0’

四、字符串比较函数

strcmp/strncmp

头文件:#include <string.h>

函数定义:int strcmp(const char *sl,const char *s2);

函数说明:

比较 s1 和 s2 指向的字符串的大小,

比较的方法:逐个字符去比较 ascII码,一旦比较出大小返回。

如果所有字符都一样,则返回 0

返回值:

如果 s1 指向的字符串大于 s2 指向的字符串 返回1

如果 s1 指向的字符串小于 s2 指向的字符串 返回-1

如果相等的话返回 0

int strncmp(const char *sl, const char *s2, size t n);

函数说明:比较 s1和 s2 指向的字符串中的前n个字符

#include <stdio.h>
#include<string.h>
int main()
{
    char s1[100]="abcdo";
    char s2[100]="abcdzfg";
    int ret=strcmp(s1,s2);
    if(ret==0)
    {
        printf("s1=s2\n");

    }
    else if(ret>0)
    {
        printf("s1>s2\n");

    }
    else
    {
        printf("s1<s2\n");
    }

    return 0;
}

注:

strcmp函数一个字符一个字符比较,只要出现不一样,就会立即返回

strncmp

#include <stdio.h>
#include<string.h>
int main()
{
    char s1[100]="hello";
    char s2[100]="helloshahhabiug";
    int ret=strncmp(s1,s2,4);
    if(ret==0)
    {
        printf("s1=s2\n");

    }
    else if(ret>0)
    {
        printf("s1>s2\n");

    }
    else
    {
        printf("s1<s2\n");
    }

    return 0;
}

 

五、字符查找函数

头文件:#include <string.h>

函数定义:char *strchr(const char *s, int c);

函数说明:

在字符指针s指向的字符串中,找 ascii 码为c的字符

注意,是首次匹配,如果过说s指向的字符串中有多个ASCII 为c的字符,则找的是第1个字符

返回值:

找到了返回找到的字符的地址,

找不到返回 NULL,

函数定义:char *strrchr(const char *s, int c);

函数的说明:末次匹配

在s指向的字符串中,找最后一次出现的 ASCII为c的字符

返回值:

末次匹配的字符的地址。

#include <stdio.h>
#include<string.h>
int main()
{
    char s2[100]="helloshahhabiug";
    char *ret=strchr(s2,'o');
    if(ret==NULL)
    {
        printf("没有找到\n");
    }
    else
    {
        printf("找到了,在数组的第%d位置\n",ret-s2);
    }

    return 0;
}

 

六、字符串匹配函数

#include <string.h>

char *strstr(const char *haystack, const char *needle);

函数说明:

在 haystack 指向的字符串中查找 needle 指向的字符串,也是首次匹配返回值:

找到了:找到的字符串的首地址

没找到:返回 NULL

七、字符串转换数值

atoi/atol/atof//字符串转换功能

头文件:#include <stdlib.h>

函数的定义:int atoi(const char *nptr);

函数的功能:

将 nptr 指向的字符串转换成整数,返回

long atol(const char *nptr);

double atof(const char *nptr);

#include <stdio.h>
#include<string.h>
int main()
{
    char s[]="9999";
    int ret=atoi(s);
    printf("ret=%d\n",ret);

    return 0;
}

八、字符串切割函数

头文件:#include <string.h>

函数定义:char *strtok(char *str, const char *delim);

函数的功能:

字符串切割,按照 delim 指向的字符串中的字符,切割 str,指向的字符串。其实就是在 str 指向的字符串中发现了 delim 字符串中的字符,就将其变成’\0’调用一次 strtok ,只切割一次,切割一次之后,再去切割的时候 strtok,的第一个参数传 NULL,意思是接着上次切割的位置继续切

注意如果 str,字符串中出现了连续的几个 delim,中的字符,则只将第一个字符变成'\0’

#include <stdio.h>
#include<string.h>
int main()
{
    char s[]="1111:222222:3333:4444:55555555555555555";
    char *ret;
    //第一次切割
    ret=strtok(s,":");
    printf("ret=%s\n",ret);
    //后面所有切割
    ret=strtok(NULL,":");
    printf("ret=%s\n",ret);
    return 0;
}

九、格式化字符串操作函数

9.1 sprintf和sscanf的基本用法

#include <stdio.h>
#include<string.h>
void test1()
{
    char buf[20];
        int a,b,c;
    sprintf(buf,"%d:%d:%d",2024,7,29);
        printf("buf=%s\n",buf);

    sscanf("2024:7:29","%d:%d:%d",&a,&b,&c);
        printf("a=%d,b=%d,c=%d\n",a,b,c);
}

int main()
{
    test1();
    return 0;
}

9.2 sscanf的高级用法

 

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

void test2()
{
    //1.跳过数据:%*s或%*d
    char buf1[20];
    sscanf("1234 56789","%*d %s",buf1);
    printf("%s\n",buf1);

    //2.读指定宽度的数据 %[width]s
    char buf2[20];
    sscanf("123456789","%5s",buf2);
    printf("%s\n",buf2);
}

int main()
{
    test2();
    return 0;
}

十、const

10.1 const修饰全局变量

1、全局变量只能使用但是不能修改

2、如果直接拿全局变量修改值,编译直接报错

3、如果使用全局变量的地址修改值,运行时程序异常结束

const int a=100;
void test()
{
    printf("a=%d\n",a);
    
    //a=666;
    //printf("a=%d\n",a);
    
    int *p=&a;
    *p=888;
    printf("a=%d\n",a);
}

 

10.2 const修饰普通局部变量

1、可以读取变量的值

2、不能直接通过变量进行修改值,编译报错

3、可以通过变量的地址修改值


void test()
{
    const int a=100;
    printf("a=%d\n",a);//100
    
    //a=666;
    //printf("a=%d\n",a);
    
    int *p=&a;
    *p=888;
    printf("a=%d\n",a);//888
}

10.3 const修饰指针变量

1、如果const修饰指针变量的类型,无法通过指针变量修改地址里面的值

2、如果const修饰指针变量,无法修改指针变量保存的地址

3、如果const既修饰指针变量的类型,又修饰指针变量,则只能通过原本变量修改值

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

void test2()
{
    int c=100;
    //const修饰指针变量的类型
    const int *p=&c;
    //const修饰指针变量
    int const *p=&c;
    //const既修饰指针变量的类型,又修饰指针变量
    const int * const p=&c;
    printf("*p=%d\n",*p);

    c=666;
    printf("*p=%d\n",*p);

    *p=777;
    printf("*p=%d\n",*p);

    int d=888;
    p=&d;
    printf("*p=%d\n",*p);
}

int main()
{
    test2();
    return 0;
}

  • 30
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值