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

博客主页: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;
} 

 运行结果:

 验证:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不掉头发的程序猿_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值