关于C语言处理字符串的基本函数整理
应昨天的承诺,我今天就来了[嘿嘿],今天就整理了一些关于处理字符串的基本函数。有不对的地方,请大佬们指出,我及时改正哦!废话不多说,直接上代码。[狗头保命]
//头文件
#include <stdio.h>
#include <stdlib.h>
//字符串处理函数
//1.1 字符串拷贝 strcpy(目标字符串,被复制字符串)
strcpy(str2,str);
//1.2 字符串中部分字符拷贝 strncpy(目标字符串,被复制字符串,字符个数)
strncpy(str2, str, 5);
//2.1 字符串追加 strcat(ch1,ch2) ch2的字符串会被追加到ch1中
strcat(ch1, ch2);
//2.2 字符串中部分字符追加 strncat(ch1,ch2) ch2的前3个字符串会被追加到ch1中
strncat(ch1, ch2,3);
//3.1 字符串比较 strcmp(cmp1,cmp2) 相同为0 大则1 小则-1
int value = strcmp(cmp1, cmp2);
//3.2 字符串中部分字符比较 strncmp(cmp1,cmp2,5) 前5个字符比较
int value2 = strncmp(cmp1, cmp2,5);
//4 字符串格式化 sprintf() sscanf()
sprintf(spr2, "hello world");
sscanf(spr, "%s", spr3);
//5.1 字符串查找 strchr()
char* p = strchr(src, dest);
//5.2 字符串查找 strstr() 返回类型为地址
int* str = strstr(src,dest);
//6 字符串分割 用\0替换标志位 但会破环源字符串
strtok();
//7 字符串类型转换 atoi() atof() atol()
atoi(dest);//转换为整数型 dest为目标字符串
atof(dest);//转换为浮点型
atol(dest);//转换为long型
未完待续哦。。。