大话C语言:第23篇 字符串常用库函数

1 strlen函数

函数说明:

#include <string.h>

size_t strlen(const char *s);
功能:计算指定指定字符串s的长度,不包含字符串结束符‘\0’
参数:
	s:字符串首地址
返回值:字符串s的长度,size_t为unsigned int类型,不同平台会不一样

代码示例:

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

int main() 
{
    char website[] = "www.whccf.cn";
    int len = strlen(website);
    printf("字符串长度 = %d\n", len);

    return 0;
}

2 strcmp函数

函数说明:

#include <string.h>

int strcmp(const char *s1, const char *s2);
功能:比较 s1 和 s2 的大小,比较的是字符ASCII码大小。
参数:
	s1:字符串1首地址
	s2:字符串2首地址
返回值:
	相等:0
	大于:>0
	小于:<0

代码示例:

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

int main() 
{
    char str1[] = "hello world";
    char str2[] = "hello world!";

    if (strcmp(str1, str2) == 0)
    {
        printf("str1==str2\n");
    } else if (strcmp(str1, str2) > 0) 
    {
        printf("str1>str2\n");
    } else 
    {
        printf("str1<str2\n");
    }

    return 0;
}

3 strcat函数

函数说明:

#include <string.h>

char *strcat(char *dest, const char *src);
功能:将src字符串连接到dest的尾部,‘\0’也会追加过去
参数:
	dest:目的字符串首地址
	src:源字符首地址
返回值:
	成功:返回dest字符串的首地址
	失败:NULL

代码示例:

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

int main()
{
    char src[15] = "one dream";
    char *dest = "one world";
    strcat(dest, src);
    printf("%s\n", dest);

    return 0;
}

4 strcpy函数

函数说明:

#include <string.h>
char *strcpy(char *dest, const char *src);
功能:把src所指向的字符串复制到dest所指向的空间中,'\0'也会拷贝过去
参数:
	dest:目的字符串首地址,如果参数dest所指的内存空间不够大,可能会造成缓冲溢出的错误情况
	src:源字符首地址
返回值:
	成功:返回dest字符串的首地址
	失败:NULL

代码示例:

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

int main() 
{
    char dest[30] = "one dream";
    char src[] = "one world";
    strcpy(dest, src);
    printf("%s\n", dest);

    return 0;
}

5 strchr函数

函数说明:

#include <string.h>
char *strchr(const char *s, int c);
功能:在字符指针s指向的字符串中,找ascii 码为c的字符
参数:
 s:指定的字符串
 c:要查找的字符
返回值:
 成功:找到的字符的地址
 失败:NULL
注意:s指向的字符串中有多个ASCII为c的字符,则找的是第1个字符

代码示例:

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

int main(int argc, char *argv[])
{
	//使用strchr函数在一个字符串中查找字符
	char s[] = "hel6lo wor6ld";
	//找第一个匹配的字符
	char *ret = strchr(s, '6');

	if(ret == NULL)
	{
		printf("没有找到\n");
	}
	else
	{
		printf("找到了,在数组的第%d个位置\n", ret ‐ s);
	}

	return 0;
}  

6 strstr函数

函数说明:

#include <string.h>
char *strstr(const char *haystack, const char *needle);
函数说明:
在haystack指向的字符串中查找needle指向的字符串,也是首次匹配
返回值:
	找到了:找到的字符串的首地址
	没找到:返回NULL

代码示例:

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

int main(int argc, char *argv[])
{
	//使用strstr函数在一个字符串中查找另一个字符串
	char s[] = "1234:4567:666:789:666:7777";

	//strstr查找的时候,查找的是第二个参数的第一个\0之前的内容
	char *ret = strstr(s, "666");

	if(ret == NULL)
	{
		printf("没找到\n");
	}
	else
	{
		printf("找到了,在当前字符串的第%d个位置\n", ret ‐ s);
	}
	
	return 0;
}

7 atoi函数

函数说明:

#include <stdlib.h>

int atoi(const char *nptr);
功能:将一个数字型字符串转化为整形数据
参数:
	nptr:指定的字符串
返回值:
	获取到的整形数据

代码示例:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
	//使用atoi将数字型字符串转化为整形数据
	char s1[] = "7856";
	int ret1 = atoi(s1);

	printf("ret1 = %d\n", ret1);

	//使用atof将浮点型的字符串转化为浮点型数据
	char s2[] = "3.1415926";
	double ret2 = atof(s2);

	printf("ret2 = %lf\n", ret2);

	return 0;
}

8 strtok函数

函数说明:

#include <string.h>
char *strtok(char *str, const char *delim);
功能:对字符串进行切割
参数:
	str:要切割的字符串
	第一次切割,就传入指定的字符串,后面所有次的切割传NULL
	delim:标识符,要根据指定的delim进行切割,切割的结果不包含delim
返回值:
	返回切割下来的字符串的首地址,如果都切割完毕,则返回NULL

代码示例:

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

int main(int argc, char *argv[])
{
	//使用strtok函数切割字符串
	char s[] = "111:22222:33:4444444444:5555555555555";
	char *ret;

	//第一次切割
	ret = strtok(s, ":");
	printf("ret = %s\n", ret);

	//后面所有切割时都要将strtok的第一个参数传NULL
	while((ret = strtok(NULL, ":")) != NULL)
	{
		printf("ret = %s\n", ret);
	}

	return 0;
}

9 sprintf和sscanf函数

函数说明:

#include <stdio.h>

int sprintf(char *str, const char *format, ...);
功能:将按照格式保存的字符串复制给str
	参数:
		str:保存字符串
		format:同printf
返回值:
	保存的字符串的字节数
#include <stdio.h>

int sscanf(const char *str, const char *format, ...);
功能:scanf是从终端读取数据并赋值给对应变量,而sscanf是从第一个参数中读取数据
	参数:
		str:指定要获取内容的字符串
		format:按照格式获取数据保存在变量中
返回值:
	成功获取的个数

代码示例:

#include <stdio.h>

void main()
{
	char buf[20];
	int year;
    int month;
    int day;

    sprintf(buf,"%d:%d:%d",2013,10,1);
    printf("buf = %s\n",buf);

    sscanf("2013:10:1", "%d:%d:%d", &year, &month, &day);
    printf("a=%d,b=%d,c=%d\n",year, month, day);
}
#include <stdio.h>

void main()
{
    //1、跳过数据:%*s或%*d
	char buf1[20];
	sscanf("1234 5678","%*d %s",buf1);
	printf("%s\n",buf1);

	//2、读指定宽度的数据:%[width]s
	char buf2[20];
	sscanf("12345678","%4s ",buf2);
	printf("%s\n",buf2);

	//3、支持集合操作:只支持获取字符串
	// %[a‐z] 表示匹配a到z中任意字符(尽可能多的匹配)
	// %[aBc] 匹配a、B、c中一员,贪婪性
	// %[^aFc] 匹配非a、F、c的任意字符,贪婪性
	// %[^a‐z] 表示读取除a‐z以外的所有字符
	char buf3[20];
	sscanf("agcd32DajfDdFF","%[a‐z]",buf3);
	printf("%s\n",buf3);
}

  • 10
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值