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

验证:

边栏推荐
- 链上治理为何如此重要,波卡Gov 2.0又会如何引领链上治理的发展?
- 微软电脑管家V2.1公测版正式发布
- 回顾历史5次经济衰退时期:这一次可能会有何不同?
- ROS 动态参数
- JSP如何使用page指令让JSP文件支持中文编码呢?
- CRS 管理与维护
- Short video seo search optimization main content
- 不要用jOOQ串联字符串
- Unknown CMake command "add_action_files"
- After an incomplete recovery, the control file has been created or restored, the database must be opened with RESETLOGS, interpreting RESETLOGS.
猜你喜欢
随机推荐
Disk and file system management
含外部储能的电力系统暂态稳定分布式控制
GIF making - very simple one-click animation tool
07-SDRAM: FIFO control module
08-SDRAM:汇总
面试高频考题解法——栈的压入弹出序列、有效的括号、逆波兰表达式求值
IO流基础
JSP如何使用request获取当前访问者的真实IP呢?
Multi-feature fusion face detection based on attention mechanism
JSP page指令errorPage属性起什么作用呢?
鲲鹏编译调试插件实战
业务测试如何避免漏测 ?
基于数据驱动的变电站巡检机器人自抗扰控制
众筹DAO“枯萎”的缩影:曾拍下《沙丘》未出版手稿的Spice DAO解散
bgp aggregation reflector federation experiment
JSP如何使用page指令让JSP文件支持中文编码呢?
Identify memory functions memset, memcmp, memmove, and memcpy
06-SDRAM : SDRAM control module
不了解SynchronousQueue?那ArrayBlockingQueue和LinkedBlockingQueue不会也不知道吧?
Arduino 基础语法







