当前位置:网站首页>c语言字符和字符串函数总结(二)
c语言字符和字符串函数总结(二)
2022-08-02 00:06:00 【BSP初级小学僧】
博客主页:https://blog.csdn.net/weixin_46094737?type=blog
欢迎评论留言 如有错误敬请指正!
本文由小学生廉原创,首发于 CSDN
未来很长,值得我们全力奔赴更美好的生活!
注意:在使用字符串处理函数时,一定要使用 #include <string.h> 开头
1、字符串查找函数 strchr(s1,'n')
在对 C 语言中,字符串查找是最频繁的字符串操作之一。使用 strchr 与 strrchr 函数查找单个字符如果需要对字符串中的单个字符进行查找,那么应该使用 strchr 或 strrchr 函数。其中,strchr 函数原型的一般格式如下:
char *strchr(const char *s, int c);
查找任何一个不包含在strcharset串中的字符 (字符串结束符null除外) 在string串中首次出现的位置指针. 返回一个指针, 指向非strcharset中的字符在string中首次出现的位置.。查找字 串string中首次出现的位置, null结束符也包含在查找中. 返回一个指针, 指向字符c在字符串string中首次出现的位置, 如果没有找到, 则返回null.。 //删除文件名,只获得路径其中使用_tcsrchr这样一个函数这是一个查找函数,从字符串中查找一个字符最后出现的位置,返回一个char*。
相对于 strchr 函数,strrchr 函数原型的一般格式如下:
char *strrchr(const char *s, int c);
与 strchr 函数一样,它同样表示在字符串 s 中查找字符 c,返回字符 c 第一次在字符串 s 中出现的位置,如果未找到字符 c,则返回 NULL。但两者唯一不同的是,strrchr 函数在字符串 s 中是从后到前(或者称为从右向左)查找字符 c,找到字符 c 第一次出现的位置就返回,返回值指向这个位置.
strchr:正常的正向查找,打印出该字符第一次出现的后面的字符串(打印包含查找的字符)
strrchr:从最后一个字符开始反向查找,打印出该字符第一次出现的后面的字符串(打印包含查找的字符)
示例:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char s1[]="happy new year!";
char *p=NULL;
p=strchr(s1,'n');//正常的正向查找,打印出该字符第一次出现的后面的字符串(打印包含查找的字符)
printf("查找字符'n':%s\n",p);
p=strrchr(s1,'a');//从最后一个字符开始反向查找,打印出该字符第一次出现的后面的字符串(打印包含查找的字符)
printf("反向查找字符'a':%s\n",p);
//puts(s1);
return 0;
} 运行结果:

2、计算字符串长度函数 :strlen
该函数可以直接计算一串字符的字符个数:ret=strlen(s1);
示例:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char s1[]="happy";
int ret=0;
ret=strlen(s1);
printf("s1字符串的长度为:%d",ret);
return 0;
} 运行结果:

3、字符串小写转大写函数:strupr
需要注意的是,如果要转换的字符串中有大写字母,则无变化。
示例:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char s1[]="Happy";
char *p=NULL;
p=strupr(s1);
puts(p);
return 0;
} 运行结果:

4、字符串大写转小写函数:strlwr
需要注意的是,如果要转换的字符串中有小写字母,则无变化。
示例:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char s1[]="HAPPy";
char *p=NULL;
p=strlwr(s1);
puts(p);
return 0;
} 运行结果:

5、字符串转数字函数(整型):atoi
该函数作用就是将一串字符型的数字转化为十进制数。
示例:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char arr[100]={"123456987"};
int ret;
ret=atoi(arr);
printf("%d", ret);
return 0;
} 运行结果:

可以看到最后打印我们是用%d来控制的。
6、字符串转数字函数(浮点型):atof
该函数作用就是将一串字符型的数字转化为十进制浮点数。
示例:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char arr[100]={"51655.51545"};
double ret=0;
ret=atof(arr);
printf("%.10f", ret);
return 0;
} 运行结果:

可以看到最后打印我们是用%f来控制的。
7、字符串转数字函数(指针型) :strtod
示例:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char arr[50]={"15415.45bf544"};
double ret=0;
char *pum=NULL;
ret=strtod(arr,&pum);//将能转换的字符返回给ret
printf("可以进行转化的字符:%f\n",ret);
printf("不可以进行转化的字符:%s",pum);//将不能转换的字符返回给p
return 0;
} 运行结果:

8、字符串转进制数函数:strtoul
示例:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char arr[]={"246543.123"};
int ret;
char *p;
ret=strtoul(arr,&p,8);
printf("%s\n",p);
printf("%x", ret);
return 0;
} 运行结果:

验证:

边栏推荐
- Interview high-frequency test questions solution - stack push and pop sequence, effective parentheses, reverse Polish expression evaluation
- 双队列实现栈?双栈实现队列?
- Axure tutorial - the new base (small white strongly recommended!!!)
- els block boundary deformation processing
- What is the function of the JSP out.println() method?
- Short video SEO search operation customer acquisition system function introduction
- Quick solution for infix to suffix and prefix expressions
- [Headline] Written test questions - minimum stack
- JSP 如何获取request对象中的路径信息呢?
- JSP request对象功能详解说明
猜你喜欢

TCL: Pin Constraints Using the tcl Scripting Language in Quartus

【加密周报】经济衰退在加息气氛中蔓延 美联储“放手一搏”?盘点上周加密市场发生的重大事件

利用“栈”快速计算——逆波兰表达式

图解LeetCode——1161. 最大层内元素和(难度:中等)

已知中序遍历数组和先序遍历数组,返回后序遗历数组

Unity—四元数、欧拉角API+坐标系统

一篇永久摆脱Mysql时区错误问题,idea数据库可视化插件配置

PHP从txt文件中读取数据的方法

为什么要使用MQ消息中间件?这几个问题必须拿下

How to solve the error when mysql8 installs make
随机推荐
How does JSP use the page command to make the JSP file support Chinese encoding?
JSP out.print()和out.write()方法的不同之处
460. LFU cache
els strip deformation
The Statement update Statement execution
When Netflix's NFTs Forget Web2 Business Security
After an incomplete recovery, the control file has been created or restored, the database must be opened with RESETLOGS, interpreting RESETLOGS.
认识USB、Type-C、闪电、雷电接口
Study Notes: The Return of Machine Learning
How to design a circular queue?Come and learn~
Double queue implementation stack?Dual stack implementation queue?
07-SDRAM :FIFO控制模块
06-SDRAM :SDRAM控制模块
零基础如何学习单片机,一位入门者的进阶路径,可参考
146. LRU 缓存
bgp 聚合 反射器 联邦实验
单片机遥控开关系统设计(结构原理、电路、程序)
An Enhanced Model for Attack Detection of Industrial Cyber-Physical Systems
基于注意力机制的多特征融合人脸活体检测
如何发现新的潜力项目?工具推荐