当前位置:网站首页>C language character and string function summary (2)
C language character and string function summary (2)
2022-08-02 00:32:00 【BSP Junior Primary School Monk】
博客主页:https://blog.csdn.net/weixin_46094737?type=blog
欢迎评论留言 如有错误敬请指正!
本文由小学生廉原创,首发于 CSDN
未来很长,值得我们全力奔赴更美好的生活!
注意:在使用字符串处理函数时,一定要使用 #include <string.h> 开头
1、字符串查找函数 strchr(s1,'n')
在对 C 语言中,字符串Lookups are one of the most frequent string operations.使用 strchr 与 strrchr The function finds a single character If you need to find a single character in a string,那么应该使用 strchr 或 strrchr 函数.其中,strchr The general format of a function prototype is as follows:
char *strchr(const char *s, int c);
查找任何一个不包含在strcharset串中的字符 (字符串结束符null除外) 在string串中首次出现的位置指针. 返回一个指针, 指向非strcharset中的字符在string中首次出现的位置..查找字 串string中首次出现的位置, null结束符也包含在查找中. 返回一个指针, 指向字符c在字符串string中首次出现的位置, 如果没有找到, 则返回null.. //删除文件名,Only get the path which is used_tcsrchrSuch a function is a lookup function,Find the last occurrence of a character in a string,返回一个char*.
相对于 strchr 函数,strrchr The general format of a function prototype is as follows:
char *strrchr(const char *s, int c);
与 strchr 函数一样,It is also represented in strings s 中查找字符 c,返回字符 c 第一次在字符串 s 中出现的位置,如果未找到字符 c,则返回 NULL.但两者唯一不同的是,strrchr 函数在字符串 s 中是从后到前(或者称为从右向左)查找字符 c,找到字符 c 第一次出现的位置就返回,返回值指向这个位置.
strchr:normal forward lookup,Print out the string that follows the first occurrence of the character(Print the characters that contain the search)
strrchr:Search backwards starting from the last character,Print out the string that follows the first occurrence of the character(Print the characters that contain the search)
示例:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char s1[]="happy new year!";
char *p=NULL;
p=strchr(s1,'n');//normal forward lookup,Print out the string that follows the first occurrence of the character(Print the characters that contain the search)
printf("查找字符'n':%s\n",p);
p=strrchr(s1,'a');//Search backwards starting from the last character,Print out the string that follows the first occurrence of the character(Print the characters that contain the search)
printf("反向查找字符'a':%s\n",p);
//puts(s1);
return 0;
}
运行结果:
2、计算字符串长度函数 :strlen
This function can directly calculate the number of characters in a string of characters: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、String lowercase to uppercase function:strupr
需要注意的是,If there are uppercase letters in the string to be converted,则无变化.
示例:
#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、String uppercase to lowercase function:strlwr
需要注意的是,If there are lowercase letters in the string to be converted,则无变化.
示例:
#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
The function of this function is to convert a string of character numbers into decimal numbers.
示例:
#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;
}
运行结果:
You can see that the last print we use%d来控制的.
6、字符串转数字函数(浮点型):atof
The function of this function is to convert a string of character numbers into decimal floating point numbers.
示例:
#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;
}
运行结果:
You can see that the last print we use%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);//Return convertible characters to ret
printf("Characters that can be converted:%f\n",ret);
printf("Characters that cannot be converted:%s",pum);//Return unconverted characters to p
return 0;
}
运行结果:
8、String to decimal number function: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;
}
运行结果:
验证:
边栏推荐
- 双队列实现栈?双栈实现队列?
- Difference between JSP out.print() and out.write() methods
- 工业信息物理系统攻击检测增强模型
- NodeJs, all kinds of path
- Graphical LeetCode - 1161. Maximum Sum of In-Layer Elements (Difficulty: Moderate)
- Transient Stability Distributed Control of Power System with External Energy Storage
- ROS 动态参数
- 基于超参数自动寻优的工控网络入侵检测
- 图解LeetCode——1161. 最大层内元素和(难度:中等)
- 测试用例:四步测试设计法
猜你喜欢
Stapler:1 靶机渗透测试-Vulnhub(STAPLER: 1)
回顾历史5次经济衰退时期:这一次可能会有何不同?
如何设计循环队列?快进来学习~
TCL:在Quartus中使用tcl脚本语言进行管脚约束
Unknown CMake command "add_action_files"
[Solution] Emqx startup under win10 reports Unable to load emulator DLL, node.db_role = EMQX_NODE__DB_ROLE = core
Don't know about SynchronousQueue?So ArrayBlockingQueue and LinkedBlockingQueue don't and don't know?
Arduino Basic Syntax
Double queue implementation stack?Dual stack implementation queue?
Redis - message publish and subscribe
随机推荐
Don't know about SynchronousQueue?So ArrayBlockingQueue and LinkedBlockingQueue don't and don't know?
ROS dynamic parameters
Axure tutorial - the new base (small white strongly recommended!!!)
面试高频考题解法——栈的压入弹出序列、有效的括号、逆波兰表达式求值
bgp 聚合 反射器 联邦实验
06-SDRAM : SDRAM control module
uni-app project summary
async/await 原理及执行顺序分析
使用jOOQ将Oracle风格的隐式连接自动转换为ANSI JOIN
控制电机的几种控制电路原理图
Redis-消息发布订阅
The Statement update Statement execution
go语言标准库fmt包怎么使用
Collection of NFT tools
Identify memory functions memset, memcmp, memmove, and memcpy
els 方块变形
磁盘与文件系统管理
08-SDRAM: Summary
Mean Consistency Tracking of Time-Varying Reference Inputs for Multi-Agent Systems with Communication Delays
JSP out.println()方法具有什么功能呢?