我们都知道,在c语言或者是c++中,都有各自的字符串操作函数,各个有各自的特点,也有类似点,今天我就给大家概括一下strlen、strcpy/strncpy、strcat/strncat、strcmp/strncmp、strchr/strrchr、strstr、strtok、atoi/atol/atof/itoa/ltoa/ftoa这些函数的作用以及使用方。
目录
7、atoi / atol / atof / itoa / ltoa / ftoa(字符串转换数值 / 数值转换字符串)
1、strlen(获取字符串长度函数)
头文件:include<string.h>
函数定义:szie_t strlen(const char *s);
函数功能:测字符指针s指向的字符串中字符的个数,不包括'\0'
返回值:字符串中字符个数
例子:
#include <stdio.h>
#include<string.h>
int main()
{
//strlen获取的字符串的长度遇到第一个\0结束
char s1[100] = "hello";
//获取s1的字符串长度
printf("s1_len = %d\n",strlen(s1));
//获取申请总空间的大小(char型数据一个数据占一个字节)
printf("s1_size = %d\n",sizeof(s1));
char *s2 = "hello";
printf("s2_len = %d\n",strlen(s2));
//sizeof获取的是*s2这个指针的地址大小
printf("s2_size = %d\n",sizeof(s2));
return 0;
}
2、strcpy / strncpy(字符串拷贝函数)
头文件:include<string.h>
函数定义:char *strcpy(char *dest, const char *src);
函数功能:拷贝src指向的字符串到dest指针指向的内存中,'\0'也会拷贝
返回值:目的内存的地址
注意:在使用时需保证dest指向的内存空间足够大,否则回出现内存污染
例子:
#include <stdio.h>
#include<string.h>
int main()
{
char s1[32] = "hello world";
char s2[32] = "abcded";
strcpy(s1,s2);
printf("s1 = %s\n",s1);
//验证s1[]后面到底是什么内容
//其实hello world后面的字符还存在,但是strcpy这个函数将\0也复制
//所以未显示后面内容
for(int i=0;i<32;i++)
{
printf("[%c] - %d\n",s1[i],s1[i]);
}
return 0;
}
函数定义:char *strncpy(char *dest, const char *src,size_t n);
函数功能:将src指向的字符串的前n个字符拷贝到dest指向的内存中
返回值:目的内存的首地址
注意:1、strncpy不能拷贝'\0'
2、如果n大于src指向的字符串中字符的个数,则在dest后面填充n - strlen(src)个'\0',也就是多出多少个就添加多少个'\0'
例子:使用方法和strcpy一致,只是多了一个参数n,就不给大家演示了,有兴趣的同学可以去验证一下注意中的两点
#include <stdio.h>
#include<string.h>
int main()
{
char s1[32] = "hello world";
char s2[32] = "abcded";
strncpy(s1,s2,3);
printf("s1 = %s\n",s1);
return 0;
}
3、strcat / strncat(字符串追加函数)
头文件:include<string.h>
函数定义:char *strcat(char *dest, const char *src);
函数功能:追加src字符串到dest指向的字符串后面,追加的时候也会追加'\0'
注意:在使用时需保证dest指向的内存空间足够大
例子:
#include <stdio.h>
#include<string.h>
int main()
{
char s1[32] = "hello world";
char s2[32] = "good";
strcat(s1,s2);
printf("s1 = %s\n",s1);
return 0;
}
函数定义:char *strncat(char *dest, const char *src,size_t n);
函数功能:追加src指向的字符串的前n个字符,到dest指向的字符串后面
注意:1、追加时会追加'\0'
2、如果n大于src指向的字符串中字符的个数,则只将src字符串追加到dest指向的字符串的后面
例子:大家自己尝试哦,只是比strcat多了个参数
4、strcmp / strncmp(字符串比较函数)
头文件:include<string.h>
函数定义:char *strcmp(const char *s1, const char *s2);
函数功能:比较s1和s2指向的字符串大小(比较方法:逐个字符比较ASCII码,一旦比较除大小立即返回)
返回值:如果s1指向的字符串大于s2指向的字符串,返回 1
如果s1指向的字符串小于s2指向的字符串,返回 -1
相等则返回 0
注意:在使用时需保证dest指向的内存空间足够大
例子:
#include <stdio.h>
#include<string.h>
int main()
{
char s1[] = "hello";
char s2[] = "w";
//我们先打印一下'h'和'w'的ASCII码
printf("h = %d\n", 'h');
printf("w = %d\n", 'w');
int ret = strcmp(s1,s2);
if(ret > 0)
{
printf("s1>s2");
}
else if(ret < 0)
{
printf("s1<s2");
}
else{
printf("s1 == s2");
}
return 0;
}
函数定义:char *strncmp(const char *s1, const char *s2,size_t n);
函数功能:比较s1和s2指向字符串中前n个字符
例子:大家自己尝试哦,只是比strcmp多了个参数
5、strchr / strrchr(字符查找函数)
头文件:include<string.h>
函数定义:char *strchr(const char *s, int c);
函数功能:在字符指针s指向的字符串中,查找第一次出现的ASCII码为c的字符
返回值:找到了返回找到字符的地址
找不到返回NULL
注意:是首次匹配,如果s指向的字符串中有多个ASCII为c的字符,则找的是第一个字符的返回值
例子:
#include <stdio.h>
#include<string.h>
int main()
{
char s1[] = "hel6lo a6ds";
char *ret = strchr(s1, '6');
if(ret == NULL)
{
printf("not found\n");
}
else
{
printf("pos = %d\n ",ret - s1);
}
return 0;
}
函数定义:char *strrchr(const char *s, int c);
函数功能:在字符指针s指向的字符串中,查找最后一次出现的ASCII码为c的字符
返回值:找到了返回末次匹配字符的地址
找不到返回NULL
注意:末次匹配
例子:
#include <stdio.h>
#include<string.h>
int main()
{
char s1[] = "hel6lo a6ds";
char *ret = strrchr(s1, '6');
if(ret == NULL)
{
printf("not found\n");
}
else
{
printf("pos = %d\n ",ret - s1);
}
return 0;
}
6、strstr(字符串匹配函数)
头文件:include<string.h>
函数定义:char *strstr(const char *s1, const char *s2);
函数功能:在s1指向的字符串中查找s2指向的字符串,也是首次匹配
返回值:找到了返回找到字符串的首地址
找不到返回NULL
例子:
#include <stdio.h>
#include<string.h>
int main()
{
char s[] = "hello llllse";
char *ret = strstr(s , "llll") ;
if(ret == NULL)
{
printf("not found\n");
}
else
{
//返回其所在位置
printf("pos = %d\n", ret -s);
}
return 0;
}
7、atoi / atol / atof / itoa / ltoa / ftoa(字符串转换数值)
头文件:include<stdlib.h>
函数定义:
int atoi(const char *p);//将p指向的字符串转换为int型并返回
int atol(const char *p);//将p指向的字符串转换为long型并返回
int atof(const char *p);//将p指向的字符串转换为float型并返回
例子:这里就只呈现atoi的例子,其他函数都是一样的,大家类比一下就行了
#include <stdio.h>
#include<stdlib.h>
int main()
{
char s[] = "333423342";
int num =atoi(s);
printf("num = %d\n",num);
return 0;
}
函数定义:
char *itoa(int value, char *string, int radix);//将int型转换为字符串并返回(value:想转换的值,*string:转换后指向字符串的指针,radix:进制)
char *ltoa(long value,char *string,int radix);//将long型转换为字符串并返回(value:想转换的值,*string:转换后指向字符串的指针,radix:进制)
char *ftoa(float value,char *string,int radix);//将float型转换为字符串并返回(value:想转换的值,*string:转换后指向字符串的指针,radix:进制)
例子:这里就只呈现itoa的例子,其他函数都是一样的,大家类比一下就行了
#include <stdio.h>
#include<stdlib.h>
int main()
{
int num = 103230;
char s[100] = " ";
itoa(num, s, 10);
printf("s = %s\n", s);
return 0;
}
8、strtok(字符串切割函数)
头文件:include<string.h>
函数定义:char *strtokchar (*s1, const char *s2);
函数功能:按照s2指向的字符串中的字符,切割s1指向的字符串。本质就是在s1指向的字符串中发现s2字符串中的字符,就将其变为'\0'
注意:1、调用一次strtok只切割一次,之后再去切割时strtok的第一个参数需传NULL(就是接着上次切割的位置继续切割)
2、如果字符s1中出现了连续的几个s2中的字符,则只将第一个变为'\0'
例子:
#include <stdio.h>
#include<string.h>
int main()
{
char s[] = "111;222;333;444";
char *ret ;
//第一次切割
ret = strtok(s , ";");
printf("ret = %s\n",ret);
//连续切割
while((ret = strtok(NULL , ";")) != NULL)
{
printf("ret = %s\n",ret);
}
return 0;
}
总结:
这只是一个概括和简略版的介绍,还没有具体的介绍其原理以及一些更细节的地方,但是我相信应该能帮到大家了。这些函数的使用时机也需要大家自己在练习的时候自己琢磨。