C语言基础之字符串处理函数

字符串处理函数

1、测量字符串长度strlen

	#include <string.h>
	size_t strlen(const char *s);

s指需要测量的字符串首元素地址

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

int main(int argc, char const *argv[])
{
    char str[128] = "hello world";
    printf("%d",strlen(str));
    return 0;
}

2、字符串拷贝函数strcpy、strncpy

(1)strcpy

    #include <string.h>
    char *strcpy(char *dest, const char *src);

​ dest:目的空间地址

​ src:原字符串首元素地址

​ 返回值:目的空间地址的首地址(新字符串地址)

​ 如果遇到’\0’,直接结束

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

int main(int argc, char const *argv[])
{
    char str[128] = "hello world";
    char str2[128] = "";
    strcpy(str2,str);
    printf("%s\n",str2);
    return 0;
}

(2)strncpy

#include <string.h>
char *strncpy(char *dest, const char *src, size_t n);

​ dest:目的空间地址

​ src:原字符串首元素地址

​ n :拷贝前n个,如果遇到’\0’,直接结束

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

int main(int argc, char const *argv[])
{
    char str[128] = "hello world";
    char str2[128] = "";
    strncpy(str2,str,6);
    printf("%s\n",str2);
    return 0;
}

3、字符串追加函数strcat、strncat

(1)strcat

#include <string.h>
char *strcat(char *dest, const char *src);

​ 将src指向的字符串追加到dest指向的字符串尾部

​ 如果遇到’\0’,直接结束

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

int main(int argc, char const *argv[])
{
    char dst[128] = "hello ";
    char str[128] = "world";
    strcat(dst,str);
    printf("%s\n",dst);
    return 0;
}

(2)strncat

#include <string.h>
char *strncat(char *dest, const char *src, size_t n);

​ 将src指向的前n个字符串追加到dest指向的字符串尾部

​ 如果遇到’\0’,直接结束

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

int main(int argc, char const *argv[])
{
    char dst[128] = "hello ";
    char str[128] = "world";
    strncat(dst,str,3);
    printf("%s\n",dst);
    return 0;
}

4、字符串比较函数strcmp、strncmp

(1)strcmp

#include <string.h>
int strcmp(const char *s1, const char *s2);

​ s1字符串 s2字符串

​ 返回值

​ >0 s1>s2

​ <0 s1<s2

​ =0 s1=s2

逐个字符比较,相同比较下一个,不同输出s1 - s2的ascll值

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

int main(int argc, char const *argv[])
{
    char dst[128] = "ab";
    char str[128] = "bb";
    printf("%d\n",strcmp(dst,str));
    return 0;
}

(2)strncmp

#include <string.h>
int strncmp(const char *s1, const char *s2, size_t n);

​ s1字符串 s2字符串

​ n:比较前n个

​ 返回值

​ >0 s1>s2

​ <0 s1<s2

​ == s1=s2

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

int main(int argc, char const *argv[])
{
    char dst[128] = "hello ";
    char str[128] = "world";
    printf("%d\n",strncmp(dst,str,3));
    return 0;
}

5、字符查找函数strchr、strrchr

(1)strchr

#include <string.h>
char *strchr(const char *s, int c);

​ 在字符串s 查找第一次出现的字符 c

​ 返回:

​ 成功返回地址

​ 失败返回NULL

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

int main(int argc, char const *argv[])
{
    char dst[] = "bvaa";
    char c = 'a';
    char *result = strchr(dst,c);
    printf("%d\n",result - dst);//打印字符所在位置
    return 0;
}

(2)strrchr

#include <string.h>
char *strrchr(const char *s, int c);

​ 在字符串s 查找最后一次出现的字符 c

​ 返回:

​ 成功返回地址

​ 失败返回NULL

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

int main(int argc, char const *argv[])
{
    char dst[] = "bvaaasd";
    char c = 'a';
    char *result = strrchr(dst,c);
    printf("%d\n",result - dst);//打印字符所在位置
    return 0;
}

6、字符串查找函数strstr

#include <string.h>
char *strstr(const char *haystack, const char *needle);

​ 返回值:

​ 找到返回找到的地址

​ 失败返回NULL

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

int main(int argc, char const *argv[])
{
    char str[] = "heloalol worlod";
    char *ret = strstr(str, "lol");
    if(ret != NULL)
    {
        printf("%s\n",ret);
        printf("%d\n",ret - str);//打印字符所在位置
    }
    return 0;
}

7、字符串转换

	#include <stdlib.h>

​ atoi将字符串 转成 int类型

​ atol将字符串 转成 long类型

​ atof将字符串 转成 float类型

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

int main(int argc, char const *argv[])
{
    printf("%d\n",atoi("11"));
    printf("%ld\n",atol("123"));
    printf("%f\n",atof("3.14f"));
    return 0;
}
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
    char buf[] = "12345ass123gd";
    int sum = 0;
    int i = 0;
    while (buf[i]>='0' && buf[i]<='9')
    {
        sum = sum*10 + (buf[i] - '0');
        i++;
    }
    printf("%d\n",sum);
    return 0;
}

结果输出12345

8、字符串切割

(1)strtok

#include <string.h>
char *strtok(char *str, const char *delim);

​ 第一次切割:str必须指向 待切割的字符串的首元素地址

​ delim指向分割符”分割符”

​ 后续切割:str传空NULL

​ 返回值:

​ 成功返回子串的首元素地址

​ 失败返回NULL

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

int main(int argc, char const *argv[])
{
    char str[] = "hel,oalolw,orlo,dsdfs,df,xc,fdgd";
    char *buf[32] = {str};//存放子串首元素地址
    int i = 0;
    while (1)
    { 
        buf[i] = strtok(buf[i] , ",");
        if(buf[i] == NULL)
            break;
        i++;
    }
    i = 0;
    while (buf[i] != NULL)
    {
        printf("%s\n",buf[i++]);
    }
    return 0;
}

9、格式化字符串

组包:按照需要的格式 组成字符串

解包:解析特定格式的数据

(1)sprintf

将零散的数据 按照固定的格式 组成字符串

#include <stdio.h>
sprintf(char  *str, const char *format, ...);

sprintf函数会根据提供的格式字符串(format)将后面的参数(…)格式化为一个字符串,并将其存储在提供的目标字符串(str)中。

函数返回的是写入的字符数量,不包括字符串的结束符

#include <stdio.h>

int main(int argc, char const *argv[])
{
    int year = 2023;
    int manth = 10;
    int day = 30;   
    char buf[128] = "";
    int len = sprintf(buf,"%d-%d-%d-",year,manth,day);
    printf("len = %d ,buf = %s\n",len,buf);
    return 0;
}

结果

len = 10 ,buf = 2023-10-30

#include <stdio.h>

int main(int argc, char const *argv[])
{
    char buf[128] = "";
    sprintf(buf,"%d",1234);
    printf("buf = %s\n",buf);
    return 0;
}

(2)sscanf

①sscanf 和%d 提取’0’~ ’9’数字
#include <stdio.h>
int main(int argc, char const *argv[])
{
    char buf[128] = "2023年7月26日";
    int year = 0;
    int manth = 0;
    int day = 0;    
    sscanf(buf , "%d年%d月%d日",&year,&manth,&day);
    printf("%d %d %d\n",year,manth,day);
    return 0;
}

结果

2023 7 26

②sscanf 和%s 遇到’\0’ (空格)结束
#include <stdio.h>
int main(int argc, char const *argv[])
{
  char buf[128] = "2023年 7月26日";
  char str[128] = ""; 
  sscanf(buf,"%s",str);
  printf("%s\n",str);  
  return 0;
}

结果

2023年

③sscanf高级用法

跳过数据 % * d ,% * s

#include <stdio.h>
int main(int argc, char const *argv[])
{
  char buf[128] = "";
  sscanf("123:e1343","%*d:%s",buf);
  printf("buf = %s\n",buf);
  return 0;
}

结果

buf = e1343

读取指定宽度的数据:%[width]s %[width]d

#include <stdio.h>
int main(int argc, char const *argv[])
{
    char buf[128] = "";
    sscanf("1231343","%3s",buf);//宽度限制
    printf("buf = %s\n",buf);
    return 0;
}

结果

buf = 123

%[a-z]表示匹配a到z的任意字符

遇到不是a-z之间的字符跳出(可加条件%[a-z,0-9,A-Z])

#include <stdio.h>
int main(int argc, char const *argv[])
{
  char buf[128] = "";
  sscanf("asd12ascHI313ASDxc43","%[a-z]",buf);
  printf("buf = %s\n",buf);
  return 0;
}

结果

buf = asd

贪婪性

%[asd]匹配a,s,d中任意一员,依次输出符合数据,遇到不符合的跳出

#include <stdio.h>
int main(int argc, char const *argv[])
{
    char buf[128] = "";
    sscanf("adsdadsddcHI313ASDxc43add","%[adds]",buf);
    printf("buf = %s\n",buf);
    return 0;
}

结果

buf = adsdadsdd

% [ ^ asd]匹配非a,s,d中任意一员,匹配到a,s,d中任意一员跳出

#include <stdio.h>
int main(int argc, char const *argv[])
{
  char buf[128] = "";
  sscanf("qwfbcHI313AaSDxc43","%[^add]",buf);
  printf("buf = %s\n",buf);
  return 0;
}

结果

buf = qwfbcHI313A

**案例:**输出指定位置字符

#include <stdio.h>
int main(int argc, char const *argv[])
{
    char buf[128] = "https://www.baidu.com";
    char name[128] = "";
    char log[128] = ""; 
    sscanf(buf,"%[^:]://www.%[^.]",name,log);
    printf("name= %s \n log= %s\n",name,log);
    return 0;
}

结果

name= https
log= baidu

(3)const

①修饰普通变量为只读变量
#include <stdio.h>
int main(int argc, char const *argv[])
{
    const int num = 10;
    num = 100;//不允许修改只允许初始化
    printf("%d\n",num);
    return 0;
}
②修饰*

( * p只读 不能通过* p修改所指向的空间内容,p仍可读可写)

#include <stdio.h>
int main(int argc, char const *argv[])
{
    int num = 10;
    int data = 20;
    const *p =&num;
    num =100;
    //*p =12;//不能修改
    p = &data;//可读可写
    printf("%d\n",num);
    return 0;
}
③修饰 p

p不可修改 *p可修改

#include <stdio.h>
int main(int argc, char const *argv[])
{
    int num = 10;

    int *const p =&num;
    *p =100;
    printf("%d\n",num);
    return 0;
}

10、如有错误欢迎指正

  • 42
    点赞
  • 46
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
【优质项目推荐】 1、项目代码均经过严格本地测试,运行OK,确保功能稳定后才上传平台。可放心下载并立即投入使用,若遇到任何使用问题,随时欢迎私信反馈与沟通,博主会第一时间回复。 2、项目适用于计算机相关专业(如计科、信息安全、数据科学、人工智能、通信、物联网、自动化、电子信息等)的在校学生、专业教师,或企业员工,小白入门等都适用。 3、该项目不仅具有很高的学习借鉴价值,对于初学者来说,也是入门进阶的绝佳选择;当然也可以直接用于 毕设、课设、期末大作业或项目初期立项演示等。 3、开放创新:如果您有一定基础,且热爱探索钻研,可以在此代码基础上二次开发,进行修改、扩展,创造出属于自己的独特应用。 欢迎下载使用优质资源!欢迎借鉴使用,并欢迎学习交流,共同探索编程的无穷魅力! 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值