字符串处理函数(c)

目录

一、获取字符串长度函数

 在’hello'中增加’\0‘,可以看见长度变了

 sizeof与strlen

 如果将一个字符串常量赋值给一个指针变量

 二、字符串拷贝函数

 strcpy

 strncpy

三、字符串追加函数

strcat 

 四、字符串比较函数

  strcmp和strncmp

 五、字符串查找函数

strchr和strrchr

六、字符串匹配函数 

 七 字符串转换数值

atoi / atol / atof  //字符串转换功能

 八 字符串切割函数

九 格式化字符串操作函数

 9.1 基本用法

 9.2 sscanf高级用法

十 const

10.1 修饰普通变量,代表只读意思

10.2 const说明

 10.2.1 const修饰全局变量

10.2.2  const修饰普通局部变量 

 10.2.3 const修饰指针变量 

1、普通指针情况

 2、const修饰指针变量类型 

 3、const修饰指针变量

 4、const既修饰指针变量的类型,又修饰指针变量



一般情况下与字符串相关的函数都是以str开头

一、获取字符串长度函数

头文件: #include <string.h>
函数定义:  size_t strlen(const char *s);
函数功能:测字符指针s指向的字符串中的字符个数,不包括‘\0‘
返回值:字符串中字符个数’

 注意:

        strlen获取的字符串长度遇到第一个’\0‘结束且'\0'不算做字符串长度之中 

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


void main()
{
	//使用strlen函数获取字符串的长度 
	char s1[100] = "hello";
	
	printf("s1_len = %d\n", strlen(s1));
}

  •  在’hello'中增加’\0‘,可以看见长度变了

 strlen 获取的字符串的长度遇到第一个\0结束 

void main()
{
	//使用strlen函数获取字符串的长度 
	//strlen 获取的字符串的长度遇到第一个\0结束 
	char s1[100] = "hel\0lo";
	
	printf("s1_len = %d\n", strlen(s1));
}

  •  sizeof与strlen

sizeof:获取的是当前数组所开辟的空间大小

void main()
{
	//使用strlen函数获取字符串的长度 
	//strlen 获取的字符串的长度遇到第一个\0结束 
	char s1[100] = "hel\0lo";
	
	printf("s1_len = %d\n", strlen(s1));
	printf("s1_len = %d\n", sizeof(s1));
}

 

  •  如果将一个字符串常量赋值给一个指针变量

void main()
{
	//使用strlen函数获取字符串的长度 
	//strlen 获取的字符串的长度遇到第一个\0结束 
	char s1[100] = "hel\0lo";
	
	printf("s1_len = %d\n", strlen(s1));
	printf("s1_len = %d\n", sizeof(s1));
	
	char *s2 = "hellp" ;
	printf("s2_len = %d\n", strlen(s2));
	printf("s2_len = %d\n", sizeof(s2));
}

 二、字符串拷贝函数

  •  strcpy

头文件: #include <string.h>
函数定义:  size_t strcpy(char *dest, const char *src);
函数功能: 拷贝src指向的字符串到dest指针指向内存中,’\0‘也会拷贝+
返回值:目的内存地址

 注意

        在使用此函数时,必须保证dest指向的内存空间足够打,否则会出现内存污染(内存溢出)

        strcpy是将str字符中的第一个\0之前包括\0复制给dest

        使用strcpy函数时,必须保证第一个参数的内存足够大,否则会造成内存问题

void main()
{
	//使用strcpy函数拷贝字符串 

	char s1[100] = "hello";
	char s2[100] = "good luck" ;
	
	strcpy(s1, s2);
	
	printf("s1 = %s\n", s1);

//验证s1后面还有没有其他字符 
	int i ;
	for(i = 0; i <32; i++)
	{
		printf("[%c] - %d\n", s1[i], s1[i]);
	 } 
	
}

使用strcpy函数时,必须保证第一个参数的内存足够大,否则会造成内存问题:

void main()
{
	//使用strcpy函数拷贝字符串 

	//char s1[100] = "hello";
   //使用strcpy函数时,必须保证第一个参数的内存足够大,否则会造成内存问题
	char s1[5] = "bye";
	char s2[100] = "good luck" ;
	
	strcpy(s1, s2);
	
	printf("s1 = %s\n", s1);

//验证s1后面还有没有其他字符 
	int i ;
	for(i = 0; i <32; i++)
	{
		printf("[%c] - %d\n", s1[i], s1[i]);
	 } 
	
}

 

  •  strncpy

头文件: #include <string.h>
函数定义:  size_t strncpy(char *dest, const char *src, size_t n);
函数功能: 将str指向的字符串前n个字符,拷贝到dest指向的内存中
返回值:目的内存的首地址

注意:

  1. strncpy不拷贝’\0‘
  2. 如果n大于src指向的字符串中的字符个数,则在dest后面填充n - strlen(src)个’\0‘

三、字符串追加函数

  • strcat 

头文件: #include <string.h>
函数定义:  char *strcat(char *dest, const char *src);
函数功能: strcat函数追加src字符串到dest指向的字符串的后面,追加时会增加’\0‘

注意:

         保证dest指向的内存空间足够大

void main()
{
	char s1[32] = "bye";
	char s2[32] = "good luck" ;
	
	//strcat是从s2的\0 的位置开始追加,直到s2的第一个\0复制完毕 后结束 
	strcat(s1, s2);
	
	printf("s1 = %s\n", s1);
}

 

  •  strncat
头文件: #include <string.h>
函数定义:  char *strncat(char *dest, const char *src,size_t n);
函数功能: strcat函数追加src指向的字符串前n个字符,到dest指向的字符后面。

注意:

        如果n大于src的字符个数,则只将src字符串追加到dest指向的字符串后面,追加时会追加’\0‘

 四、字符串比较函数

  •   strcmp和strncmp

头文件: #include <string.h>
函数定义:  int strcmp(const char *s1, const char *s2);
             int strncmp(const char *s1, const char *s2, size_t n);
函数功能: 
  strcmp是比较两个字符串的内容,strncmp是比较两个字符串的前n个字节是否一样
     比较s1和s2指向的字符串大小
    比较方法:逐个字符去比较ascii码,一旦比较出大小返回
    如果所有字符都一样,则返回0

返回值:
    如果s1指向的字符串大于s2指向的字符串,返回1
    如果s1指向的字符串小于s2指向的字符串,返回-1
    如果相等的话返回0
void main()
{
	//使用strcmp比较2个字符串的内容是否一样
	//strcmp函数一个字符一个字符比较,只要出现不一样的,就会立即返回 
	char s1[] = "hello";
	char s2[] = "hella"; 
	
	int ret = strcmp(s1, s2);
	
	if(ret == 0)
	{
		printf("s1 = s2");

	}
	else if(ret > 0)
	{
		printf("s1 > s2");
	}
	else 
	{
		printf("s1 < s2");
	}	
}

void main()
{
	//使用strcmp比较2个字符串的内容是否一样
	//strcmp函数一个字符一个字符比较,只要出现不一样的,就会立即返回 
	char s1[] = "hello";
	char s2[] = "hella"; 
	
	int ret = strncmp(s1, s2, 4);
	
	if(ret == 0)
	{
		printf("s1 = s2");

	}
	else if(ret > 0)
	{
		printf("s1 > s2");
	}
	else 
	{
		printf("s1 < s2");
	}	
}

 五、字符串查找函数

  • strchr和strrchr

头文件: #include <string.h>
函数定义:  char *strchr(const char *s1, int c);
             
函数功能: 
     在字符指针s指向的字符串中,找ascii码为c的字符

注意:
    是首次匹配,如果过s指向的字符串中有多个ascii为c的字符,则找的是第一个字符

返回值:
    找到返回找到的字符地址
    找不到返回NULL



函数定义:  char *strrchr(const char *s1, int c);
             
函数功能: 
    末次匹配
     在字符指针s指向的字符串中,找到最后一次出现的ascii码为c的字符

返回值:
   末次匹配的字符地址
void main()
{
	//使用strchr函数在一个字符串中查找字符 
	char s[] = "hel6lo wor6lf";
//找第一个匹配字符
	char *ret = strchr(s, '6') ;
	
	
	if(ret == NULL)
	{
		printf("no found\n");

	}
	else 
	{
		printf("yes ,找到了在数组的第%d个位置\n", ret - s);
	}
	
}

void main()
{
	//使用strchr函数在一个字符串中查找字符 
	char s[] = "hel6lo wor6lf";
//找最后一个匹配字符
	char *ret = strrchr(s, '6') ;
	
	
	if(ret == NULL)
	{
		printf("no found\n");

	}
	else 
	{
		printf("yes ,找到了在数组的第%d个位置\n", ret - s);
	}
	
}

六、字符串匹配函数 

头文件: #include <string.h>
函数定义:  char *strstr(char *haystack, const char *needle);
函数功能: 在haystack指向的字符串中查找needle指向的字符串,也是首次匹配

返回值:
    找到了:找到的字符串的首地址
    没找到——NULL
void main()
{
	//使用strstr函数在一个字符串,查找另外一个字符串 
	char s[] = "hello world YES";
	
	//strstr查找时,查找的是第二个参数的第一个\0之前的内容 
	char *ret = strstr(s, "world") ;
	
	
	if(ret == NULL)
	{
		printf("no found\n");

	}
	else 
	{
		printf("yes ,找到了在字符串的第%d个位置\n", ret - s);
	}
	
}

 七 字符串转换数值

atoi / atol / atof  //字符串转换功能

头文件: #include <stdlin.h>
函数定义: int atoi(const char *nptr);
函数功能:将 nptr指向的字符串转换成整数

long atol(const char *nptr);
double atof(const char *nptr);
#include <stdio.h> 
#include <stdlib.h>

void main()
{
	//使用atoi将数字型字符串转化为整型数据 
	char s1[] = "123456";
	int ret1 = atoi(s1); 

	printf("ret1 = %d\n", ret1 );
	
	//用atof将浮点型字符串转化为浮点型数据 
	char s2[] = "3.1415926";
	double ret2 = atof(s2); 
	printf("ret2 = %lf\n", ret2);
}

 八 字符串切割函数

头文件: #include <string.h>
函数定义:  char *strtok(char *str, const char *delim);
函数功能: 
    字符串切割,按照delim指向的字符串中的字符,切割str指向的字符串。
其实就是在str指向的字符串中发现了delim 字符串中的字符,就将其变成"\0',
调用一次strtok只切割一次,切割一-次之后,再去切割的时候strtok的第一个参数
传NULL,意思是接着上次切割的位置继续切

返回值:
        返回切割下来的字符串的首地址,如果都切割完毕,则返回NULL。      

注意:如果str字符串中出现了连续的几个delim中的字符,则只将第一个字符变成‘\0’
void main()
{
	//使用strtok函数切割字符串 
	char s[] = "111:222:3333:444:555";
	char *ret; 

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

九 格式化字符串操作函数

int sprintf(char *buf, const char*format,...)
//输出到buf指定内存区域

int sscanf(const char*buf, const char *format,...)
//从buf指定内存区域中读入信息

 9.1 基本用法

//sprintf和 sscanf基本用法 
void test()
{
	char buf[20];
	int a, b, c;
	
	sprintf(buf, "%d:%d:%d", 2013, 10, 1);
	printf("buf = %s\n", buf);
	
	sscanf("2013:10:1","%d:%d:%d", &a,&b,&c);
	printf("a = %d,b =%d, c =%d\n", a, b, c);
}

 9.2 sscanf高级用法

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

void main()
{
	test1();
	
}

十 const

10.1 修饰普通变量,代表只读意思

const int a = 100; // 定义一个只读变量a值为100

后面程序中,不能再给a赋值了

 

10.2 const说明

 10.2.1 const修饰全局变量

//const修饰全局变量 
//此时全局变量只能使用但不能修改
//如果直接拿全局变量修改值,编译直接报错
//如果使用全局变量的地址修改值,运行时程序异常结束 
const int a = 100; 
void test()
{
	printf("a = %d\n", a);
	
	//能不能拿a去修改
	//会报错 
	/*a = 666;
	printf("a = %d\n", a);*/ 
	
	//可拿地址进行修改?
	//不能,程序异常 
	int *p = &a;
	*p = 888;
	printf("a = %d\n", a);
}

10.2.2  const修饰普通局部变量 

//const修饰普通局部变量 
//可以读取变量的值
//不能直接通过变量进行 修改值,编译报错 
// 可以通过变量的地址修改值 
void test1()
{
	const int b = 100;
	printf("b = %d\n", b);
	
/*	b = 666;
	printf("b = %d\n", b);*/
	 
	int *p = &b;
	*p = 888;
	printf("b = %d\n", b);
}

void main()
{
	test1();
	
}

 10.2.3 const修饰指针变量 

1、普通指针情况

//const修饰指针变量 
void test2()
{
	//修饰指针变量?还是修饰指针变量类型
	
	int c = 100; 
    int *p = &c; 
	printf("p = %d\n", *p); //怎么更改*p的值 
	
	//1.直接通过变量修改 
	c = 666;
	printf("p = %d\n", *p); 
	
	//2.直接通过*p修改 
	*p = 777;
	printf("p = %d\n", *p); 
	
	//3、让p保存新变量的 地址 
	int d = 888;
	p = &d; 
	printf("p = %d\n", *p); 
 } 

void main()
{
	test2();
	
}

 

 2、const修饰指针变量类型 

//const修饰指针变量 
void test2()
{
	//修饰指针变量?还是修饰指针变量类型
	
	//const修饰指针变量类型 
	//如果const修饰指针变量的类型 ,无法通过指针变量修改地址里面的值 
	int c = 100; 
	const int *p = &c; 
	printf("p = %d\n", *p); //怎么更改*p的值 
	
	//1.直接通过变量修改 
	c = 666;
	printf("p = %d\n", *p); 
	
	//2.直接通过*p修改,此处报错 
	/**p = 777;
	printf("p = %d\n", *p); */
	
	//3、让p保存新变量的 地址 
	int d = 888;
	p = &d; 
	printf("p = %d\n", *p); 
 } 

void main()
{
	test2();
	
}

 3、const修饰指针变量

//const修饰指针变量 
void test2()
{
	//修饰指针变量?还是修饰指针变量类型
	
	//const修饰指针变量
	//如果const修饰指针变量,无法修改指针变量保存的地址 

	int c = 100; 
 	int * 	const p = &c; 
	printf("p = %d\n", *p); //怎么更改*p的值 
	
	//1.直接通过变量修改 
	c = 666;
	printf("p = %d\n", *p); 
	
	//2.直接通过*p修改,
	*p = 777;
	printf("p = %d\n", *p); 
	
	//3、让p保存新变量的 地址 ,此处报错  
/*	int d = 888;
	p = &d; 
	printf("p = %d\n", *p); */
 } 

void main()
{
	test2();
	
}

 4、const既修饰指针变量的类型,又修饰指针变量

//const修饰指针变量 
void test2()
{
	//修饰指针变量?还是修饰指针变量类型
	
	//const修饰指针变量 + 修饰指针变量类型 
	//如果const既修饰指针变量的类型,又修饰指针变量,则只能通过原本变量 修改值 
	int c = 100; 
 	const int * const p = &c; 
	printf("p = %d\n", *p); //怎么更改*p的值 
	
	//1.直接通过变量修改 
	c = 666;
	printf("p = %d\n", *p); 
	
	//2.直接通过*p修改,此处报错  
	/**p = 777;
	printf("p = %d\n", *p);*/ 
	
	//3、让p保存新变量的 地址 ,此处报错  
	/*int d = 888;
	p = &d; 
	printf("p = %d\n", *p); */
 } 

void main()
{
	test2();
	
}


版权声明:本文为qq_41070511原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。