C语言:字符串常用函数:

 本篇文章介绍了字符串最最最...最常用的函数,看到的朋友们一定不要错过哦,全是干货,记得收藏起来,别等想要的时候找不到了!

所给的测试样例只是例子, 小伙伴们也可以自己用不同的数据测试哦!

目录

1.strlen函数:

2.strcpy函数:

3.strcmp函数:

4.strcat函数:

5.strstr函数:

6.strlwr函数:

7.strupr函数:

8.strset函数:

9.gets函数:

10.puts函数:

1.strlen函数:

用于判断字符串的长度

代码如下:大家也可以自己试一下:

#include <stdio.h>
#include <string.h>

int main()
{
	char s[10]="asdfg";
	int len = strlen(s);
	
	printf("%d",len);
    return 0;
} 

2.strcpy函数:

用于将字符串2复制给字符串1.

 代码段:

#include <stdio.h>
#include <string.h>

int main()
{
	char s1[10]="asdfg";
	char s2[10]="qwert";
	
	strcpy(s1,s2);
	printf("%s",s1);
	return 0;
}

3.strcmp函数:

用于比较两个字符串。

 

代码段:分为两个字符串相同和不同的情况,相同的两个字符串输出0,不同的两个字符串输出-1.

#include <stdio.h>
#include <string.h>

int main()
{
	char s1[10]="asdfg";
	char s2[10]="qwert";
	
	int a = strcmp(s1,s2);
	
	printf("%d",a);
	return 0;
}
#include <stdio.h>
#include <string.h>

int main()
{
	char s1[10]="asdfg";
	char s2[10]="asdfg";
	
	int a = strcmp(s1,s2);
	
	printf("%d",a);
	return 0;
}

4.strcat函数:

将字符串2连接到字符串1的后面.

代码段:

#include <stdio.h>
#include <string.h>

int main()
{
	char s1[10]="abc";
	char s2[10]="def";
	
	strcat(s1,s2);
	printf("%s",s1);
	return 0;
}

5.strstr函数:

用于找到子串在一个字符串中第一次出现的位置;

代码:

#include <stdio.h>
#include <string.h>

int main()
{
	char s1[10]="asdfghj";
	char s2[10]="sd";
	
	int *p=strstr(s1,s2);
	if(p==NULL)
	{
		printf("Not Found\n");
	}
	else
	{
		printf("%s\n",p);
	}
	return 0;
}

6.strlwr函数:

将字符串中的大写字母转为小写字母;

代码: 

#include <stdio.h>
#include <string.h>

int main()
{
	char s1[10]="asDFGhjk";
	
	strlwr(s1);
	printf("%s",s1);
} 

7.strupr函数:

将字符串中的小写字母转为大写字母;

 

代码: 

#include <stdio.h>
#include <string.h>

int main()

{	char s1[10]="qweRTYui";
	
	strupr(s1);
	printf("%s",s1);
	
	return 0;
}

8.strset函数:

将字符串中的所有替换为给定的字符;

 

#include <stdio.h>
#include <string.h>

int main()
{
	char s[20]="Hello World!";
	char *p;
	
	p=strset(s,'m');
	
	printf("%s",p);
	return 0;
}

9.gets函数:

输入字符串;

 

代码: 

#include <stdio.h>
#include <string.h>

int main()
{
	char s[10];
	gets(s);
	printf("%s",s); 
	return 0;
}

10.puts函数:

输出字符串;

 

代码: 

#include <stdio.h>
#include <string.h>

int main()
{
	char s[10];
	gets(s);
	puts(s);
	
	return 0;
}

 

 

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

是小蟹呀^

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

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

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

打赏作者

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

抵扣说明:

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

余额充值