[字符串处理函数 part 2]

八、字符串切割函数

#include <string.h>
char *strtok(char *str, const char *delim);
  • 功能:对字符串进行切割
  • 参数:
    • str:要切割的字符串
    • 第一次切割,就传入指定的字符串,后面所有次的切割传NULL
    • delim:标识符,要根据指定的delim进行切割,切割的结果不包含delim
  • 返回值:返回切割下来的字符串的首地址,如果都切割完毕,则返回NULL
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
    
    
    //使用strtok函数切割字符串
    char s[] = "111:22222:33:4444444444:5555555555555";
    char *ret;

    //第一次切割
    ret = strtok(s, ":");
    printf("ret = %s\n", ret);

    //后面所有切割时都要将strtok的第一个参数传NULL
    while((ret = strtok(NULL,":")) != NULL)
    {
    
    
        printf("ret = %s\n", ret);
    }

    return 0;
}

执行结果:
执行结果strtok

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

#include <stdio.h>
int sprintf(char *str, const char *format, ...);
  • 功能:将按照格式保存的字符串复制给str
  • 参数:
    • str:保存字符串
    • format:同printf
  • 返回值:保存的字符串的字节数
#include <stdio.h>
int sscanf(const char *str, const char *format, ...);
  • 功能:scanf是从终端读取数据并赋值给对应变量,而sscanf是从第一个参数中读取数据
  • 参数:
    • str:指定要获取内容的字符串
    • format:按照格式获取数据保存在变量中
  • 返回值:成功获取的个数

sprintf和sscanf的基本用法

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

//sprintf和sscanf的基本用法
void sprintf_sscanf()
{
    
    
    char buf[20];
    int a, b, c;

    sprintf(buf, "%d:%d:%d", 2023, 04, 18);
    printf("buf = %s\n", buf);

    sscanf("2023:04:18", "%d:%d:%d", &a, &b, &c);
    printf("a = %d, b = %d, c  = %d", a, b, c);
}

int main(int argc, char *argv[])
{
    
    
    sprintf_sscanf();
    return 0;
}

执行结果:
sprintf和sscanf

sscanf高级用法

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

//sscanf高级用法
void test2_sscanf()
{
    
    
    //1、跳过数据:%*s或%*d
    char buf1[20];

    sscanf("2023 0418", "%*d %s", buf1);
    printf("%s\n", buf1);

    //2、读指定宽度的数据:%[width]s
    char buf2[20];
    sscanf("20230418", "%4s", buf2);
    printf("%s\n", buf2);

    //3、支持集合操作:只支持获取字符串
    // %[a‐z] 表示匹配a到z中任意字符(尽可能多的匹配)
    // %[aBc] 匹配a、B、c中一员,贪婪性
    // %[^aFc] 匹配非a、F、c的任意字符,贪婪性
    // %[^a‐z] 表示读取除a‐z以外的所有字符
    char buf3[20];

    sscanf("adkjwed32DGdhhsbgfVUDYJav","%[a-z]", buf3);
    printf("%s\n", buf3);
}

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

执行结果:
sscanf高级用法

十、const

#include <stdio.h>


//const修饰全局变量
//此时全局变量只能使用但是不能修改,
//如果直接拿全局变量修改值,编译直接报错
//如果使用全局变量的地址修改值,运行时程序异常结束
const int a = 100;
void test1()
{
    
    
    printf("a = %d\n", a);

    /* 编译报错 */
    //a = 666;
    //printf(("a = %d\n", a);

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

//const修饰普通局部变量
//可以读取变量的值
//不能直接通过变量进行修改值,编译报错
//可以通过变量的地址修改值
void test2()
{
    
    
    const b = 200;
    printf("b = %d\n",b);

    /* 编译报错 */
    //b = 777;
    //printf("b = %d\n", b);

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

//const修饰指针变量
//如果const修饰指针变量的类型,无法通过指针变量修改地址里面的值
//如果const修饰指针变量,无法修改指针变量保存的地址
//如果const既修饰指针变量的类型,又修饰指针变量,则只能通过原本变量修改值
void test3()
{
    
    
    int c = 300;
    //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(int argc, char *argv[])
{
    
    
    test3();
    return 0;
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/shuting7/article/details/130229500
今日推荐