当前位置:网站首页>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;
}
运行结果:
验证:
边栏推荐
- go语言标准库fmt包怎么使用
- 不了解SynchronousQueue?那ArrayBlockingQueue和LinkedBlockingQueue不会也不知道吧?
- Short video SEO optimization tutorial Self-media SEO optimization skills and methods
- Difference between JSP out.print() and out.write() methods
- 基于超参数自动寻优的工控网络入侵检测
- 146. LRU 缓存
- TCL:在Quartus中使用tcl脚本语言进行管脚约束
- Redis-消息发布订阅
- 电机原理动图合集
- Short video seo search optimization main content
猜你喜欢
Double queue implementation stack?Dual stack implementation queue?
How to reinstall Win11?One-click method to reinstall Win11
图解LeetCode——1161. 最大层内元素和(难度:中等)
IP核:FIFO
Play NFT summer: this collection of tools is worth collecting
When Netflix's NFTs Forget Web2 Business Security
Using the "stack" fast computing -- reverse polish expression
如何优雅的消除系统重复代码
uni-app项目总结
Unity—四元数、欧拉角API+坐标系统
随机推荐
An Enhanced Model for Attack Detection of Industrial Cyber-Physical Systems
[Headline] Written test questions - minimum stack
如何发现新的潜力项目?工具推荐
Statement执行update语句
JSP out. The write () method has what function?
Don't know about SynchronousQueue?So ArrayBlockingQueue and LinkedBlockingQueue don't and don't know?
Unknown CMake command “add_action_files“
Graphical LeetCode - 1161. Maximum Sum of In-Layer Elements (Difficulty: Moderate)
JSP out.print()和out.write()方法的不同之处
JSP built-in object out object function introduction
Disk and file system management
SphereEx Miao Liyao: Database Mesh R&D Practice under Cloud Native Architecture
Is TCP reliable?Why?
中缀转后缀、前缀表达式快速解决办法
08-SDRAM:汇总
632. 最小区间
Ansible中的任务执行控制
Transient Stability Distributed Control of Power System with External Energy Storage
回顾历史5次经济衰退时期:这一次可能会有何不同?
What is the function of the JSP out.println() method?