一.字符串函数
1.strlen函数
size_t strlen (const char * str);
- strlen函数返回字符长度。即\0之前的字符个数
- 字符串以\0结尾
- 函数返回值为size_t ,是无符号的
- str可为数组名,可为指针
2.strcpy函数(复制字符串函数)
char * strcpy(char * destination, const char* str);
- 这里destination必须为数组地址
- 源字符串必须以\0结尾
char a[20];
char str[20] = "to be or not";
strcpy(a,str);
printf("%s",a);
3.strcat函数(拼接字符串函数)
char * strcat(char * destination, const char* source);
- 源字符串必须以\0结尾
- 不可以源字符串与目标字符串相同,会报错
char a[20]="to be";
char str[20] = "to be or not ";
strcat(str,a);
printf("%s",str);
4.strcmp函数(字符串比较函数)
int strcat(const char * str1, const char* str2);
- str1>str2 ,返回大于0的数
- str1=str2 ,返回0
- str1<str2 ,返回小于0的数
char a[20]="to be";
char str[20] = "to be or not ";
printf("%d",strcmp(str,a));
5.strncpy函数
char * strncpy(char * destination, const char* str,size_t num);
- num为要拷贝的字符个数
- 若字符串小于num个字符,则进行补零操作
char a[20]="to be";
char str[20] = "to be or not ";
strncpy(a, str, 8);
printf("%s",a);
6.strncat函数
char * strncat(char * destination, const char* str,size_t num);
- num为要拷贝的字符个数
- 若字符串小于num个字符,则进行补零操作
char a[20]="to be";
char str[20] = "to be or not ";
strncat(str,a, 2);
printf("%s",str);
7.strncmp函数
int strncmp(const char * str1, const char* str2,size_t num);
- 当出现字符比较不同,或比较完num个字符,或者一个字符串结束时,完成比较;
char a[20]="to be";
char str[20] = "to be or not ";
printf("%d",strncmp(a,str,5));
8.strstr函数(查字串函数)
char * strstr(const char *a , const char* b);
- 记录b串在a串中第一次出现的位置,返回该位置指针;
- 若b串不是a串的字串,则返回空;
char *str1, *str2;
char a[20]="to be";
char str[20] = "to be or not ";
str1 = strstr(a, str);
str2 = strstr(str, a);
printf("%s\n%s\n",str1,str2);
9.strtok函数(寻找字符串标记函数)
char * strtok(char *a , const char* b);
- a为目标字符串,b为所需要查找标记
- 第一次寻找时用数组名查找,之后用NULL即可。
- strtok会改变原串,所以一般需要在复制串操作
- 具体实现参照下面代码:
char *pth;
char str[20] = "to be or not ";
pth = strtok(str, " ,-*");
while (pth != NULL)
{
printf("%s", pth);
pth = strtok(NULL, " ");
}
10.strerror函数(返回错误码,所对应的错误信息)
#include<errno.h> //(必须包含此头文件 )
char * strerror(int errnum);
二.字符函数
1.字符分类函数
int isalnum(int c):检查字符是否为数字或字母;(0~9,a~z,A~Z)
int isalpha(int c):检查字符是否为字母;(a~z, A~Z)
int iscntrl(int c):检查字符是否为控制字符;(八进制000~037以及177的字符)
int isdigit(int c):检查字符是否为十进制数字;(0~9)
int isgraph(int c):检查字符是否为图形表示,依赖于使用语言的环境;0~9,a~z,A~Z,以及标点符号)
int islower(int c):检查字符是否为小写的字母;(a~z)
int isprint(int c):检查字符是否为可打印的;(数字、字母、标点符号、空白字符)
int ispunct(int c):检查字符是否为标点符号;(! ” # $ % & ’ ( ) * + , - . / : ; < = > ? @ [ ] ^ _ ` { | } ~等)
int isspace(int c):检查字符是否为空白字符;(TAB、换行、垂直TAB、换页、回车、空格)
int isupper(int c):检查字符是否为大写字母;(A~Z)
int isxdigit(int c):检查字符是否为十六进制数字;(0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e f)
2.字符转换函数
int tolower(int c):转化字符为小写字母;
int toupper(int c):转化字符为大写字母;
三.内存函数
1.memset函数(内存填充函数)
void *memset(void ,int,size_t);
- 可以填充的值:0和-1。计算机中用二进制补码表示数字,0的二进制补码为全0,-1的二进制补码为全1。
- memset() 函数常用于内存空间初始化。如:
char str[100];
memset(str,0,100);
2.memcpy函数(内存拷贝函数)
void * memcpy ( void * destination, const void * source, size_t num );
- 内存重叠问题,没有被定义
- 可以拷贝其他类型数据
int age,b;
char name[40];
char myname[] = "Pierre de Fermat";
memcpy(name, myname, strlen(myname) + 1);
b = 46;
memcpy(&age, &b, sizeof(b));
printf("person_copy: %s, %d \n", name, age);
3.memmove函数
void * memmove ( void * destination, const void * source, size_t num );
- 与memcpy的不同点在于,memmove可以实现内存重叠
4.memcmp函数
int memcmp ( const void * ptr1, const void * ptr2, size_t num );
- 比较从ptr1和ptr2开始的num个字节
- 返回值情况,与strncpy类似
#include <stdio.h>
#include <string.h>
int main()
{
char buffer1[] = "DWgaOtP12df0";
char buffer2[] = "DWGAOTP12DF0";
int n;
n = memcmp(buffer1, buffer2, sizeof(buffer1));
if (n > 0)
{
printf("'%s' is greater than '%s'.\n", buffer1, buffer2);
}
else if (n < 0)
{
printf("'%s' is less than '%s'.\n", buffer1, buffer2);
}
else
{
printf("'%s' is the same as '%s'.\n", buffer1, buffer2);
}
return 0;
}