当前位置:网站首页>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;
} 运行结果:

验证:

边栏推荐
- 图解LeetCode——1161. 最大层内元素和(难度:中等)
- Disk and file system management
- Unknown CMake command “add_action_files“
- Short video SEO optimization tutorial Self-media SEO optimization skills and methods
- 已知中序遍历数组和先序遍历数组,返回后序遗历数组
- What is it like to trade for a living?
- els strip deformation
- 双队列实现栈?双栈实现队列?
- Knowing the inorder traversal of the array and the preorder traversal of the array, return the postorder history array
- Multidimensional Correlation Time Series Modeling Method Based on Screening Partial Least Squares Regression of Correlation Variables
猜你喜欢

08-SDRAM: Summary

磁盘与文件系统管理

Using the "stack" fast computing -- reverse polish expression

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

PHP to read data from TXT file

146. LRU cache

玩转NFT夏季:这份工具宝典值得收藏

【解决】win10下emqx启动报错Unable to load emulator DLL、node.db_role = EMQX_NODE__DB_ROLE = core

Unknown CMake command “add_action_files“

回顾历史5次经济衰退时期:这一次可能会有何不同?
随机推荐
具有通信时延的多自主体系统时变参考输入的平均一致性跟踪
字符串分割函数strtok练习
JSP out.write()方法具有什么功能呢?
bgp 聚合 反射器 联邦实验
els 方块边界变形处理
How to design a circular queue?Come and learn~
基于相关性变量筛选偏最小二乘回归的多维相关时间序列建模方法
TCL: Pin Constraints Using the tcl Scripting Language in Quartus
一文概览最实用的 DeFi 工具
[头条]笔试题——最小栈
不了解SynchronousQueue?那ArrayBlockingQueue和LinkedBlockingQueue不会也不知道吧?
632. Minimum interval
els block deformation
What is the function of the JSP out.println() method?
22.支持向量机—高斯核函数
07-SDRAM :FIFO控制模块
【HCIP】BGP小型实验(联邦,优化)
电机原理动图合集
Using the "stack" fast computing -- reverse polish expression
Collection of NFT tools