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

验证:

边栏推荐
猜你喜欢

08-SDRAM: Summary

Arduino 基础语法

c语言字符和字符串函数总结(二)

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

bgp 聚合 反射器 联邦实验

Knowing the inorder traversal of the array and the preorder traversal of the array, return the postorder history array

Task execution control in Ansible
![[Headline] Written test questions - minimum stack](/img/67/08f2be8afc780e3848371a1b5e04db.png)
[Headline] Written test questions - minimum stack

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

PHP to read data from TXT file
随机推荐
基于相关性变量筛选偏最小二乘回归的多维相关时间序列建模方法
Multidimensional Correlation Time Series Modeling Method Based on Screening Partial Least Squares Regression of Correlation Variables
Knowing the inorder traversal of the array and the preorder traversal of the array, return the postorder history array
poker question
Detailed explanation of JSP request object function
146. LRU 缓存
Axure tutorial - the new base (small white strongly recommended!!!)
测试用例:四步测试设计法
JSP page指令errorPage属性起什么作用呢?
els block deformation judgment.
工业信息物理系统攻击检测增强模型
基于编码策略的电网假数据注入攻击检测
els strip deformation
利用“栈”快速计算——逆波兰表达式
460. LFU 缓存
How does JSP use the page command to make the JSP file support Chinese encoding?
Constructor, this keyword, method overloading, local variables and member variables
JSP built-in object out object function introduction
重装腾讯云云监控后如果对应服务不存在可通过sc.exe命令添加服务
【21天学习挑战赛】顺序查找和二分查找的小总结