size_t strlen ( const char * str );
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
int main()
{
char arr[] = "abc";
//char arr[] = { 'a', 'b', 'c' };
int len =strlen(arr);
printf("%d\n", len);
return 0;
}
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
int my_strlen(const char* str)
{
int count = 0;//计数器
assert(str != NULL);
while (*str != '\0')
{
count++;
str++;
}
return count;
}
int main()
{
char arr[] = "abc";
//char arr[] = { 'a', 'b', 'c' };
int len =my_strlen(arr);
printf("%d\n", len);
return 0;
}
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
int main()
{
if (strlen("abc") - strlen("abcdef") > 0)
{
printf(">\n");
}
else
{
printf("<=\n");
}
return 0;
}
char* strcpy ( char * destination , const char * source);
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
int main()
{
char arr[10] = "#####" ;
strcpy(arr, "hello");
printf("%s\n", arr);
return 0;
}
char * strcat ( char * destination , const char * source );
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
int main()
{
char arr[20] = "abcd";
strcat(arr, "efgh");//?
printf("%s\n", arr);
return 0;
}
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
#include
char* my_strcat(char* dest, const char*src)
{
char* ret = dest;
assert(dest && src);//断言
//1. 找目标字符串中的\0
while (*dest)
{
dest++;
}
//2. 追加源字符串,包含\0
while(*dest++ = *src++)
{
;
}
return ret;//返回的目标空间的起始地址
}
int main()
{
char arr1[20] = "hello ";//world
char arr2[] = "world";
printf("%s\n", my_strcat(arr1, arr2));
return 0;
}
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
int main()
{
char arr[20] = "abcd";
strcat(arr, arr);
printf("%s\n", arr);
return 0;
}
int strcmp ( const char * str1 , const char * str2 );
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
int main()
{
char* p = "abcdef";
char* q = "abb";
int ret = strcmp(p, q);
if (ret > 0)
{
printf("p > q\n");
}
else if (ret < 0)
{
printf("p < q\n");
}
else
{
printf("p == q\n");
}
return 0;
}
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
int my_strcmp(const char* s1, const char* s2)
{
assert(s1 && s2);
while (*s1 == *s2)
{
if (*s1 == '\0')
{
return 0;//相等
}
s1++;
s2++;
}
return *s1 - *s2;
}
int main()
{
char* p = "abcdef";
char* q = "abb";
int ret = strcmp(p, q);
if (ret > 0)
{
printf("p > q\n");
}
else if (ret < 0)
{
printf("p < q\n");
}
else
{
printf("p == q\n");
}
return 0;
}
长度不受限制的字符串函数 | 长度受限制的字符串函数 |
strcpy | strncpy |
strcat | strncat |
strcmp | strncmp |
char * strncpy ( char * destination , const char * source , size_t num );
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
int main()
{
char arr1[20] = "abcdefghi";
char arr2[] = "qwer";
strncpy(arr1, arr2, 2);
printf("%s\n", arr1);//qwcdef
strncpy(arr1, arr2, 6);
printf("%s\n", arr1);//qwcdef
return 0;
}
char * strncat ( char * destination , const char * source , size_t num );
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
int main()
{
char arr1[20] = "hello ";
char arr2[] = "world";
strncat(arr1, arr2, 5);
printf("%s\n", arr1);
return 0;
}
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
int main()
{
char arr1[20] = "abcdef";
strncat(arr1, arr1, 7);
printf("%s\n", arr1);
return 0;
}
补充:在使用strncat时一点要保证char * destination足够大
int strncmp ( const char * str1 , const char * str2 , size_t num );
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
int main()
{
char* p = "abcdqf";
char* q = "abcdef";
//int ret = strcmp(p, q,4);
int ret = strncmp(p, q, 5);
if (ret > 0)
{
printf(">");
}
else if (ret < 0)
{
printf("<");
}
else
{
printf("=");
}
return 0;
}
补充:strlen和sizeof返回值都是无符号的,
char * strstr ( const char * str1 , const char * str2 );
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
int main()
{
char arr1[] = "abbbcdef";
char arr2[] = "abc";
//char arr2[] = "bbc";
//在arr1中查找是否包含arr2数组
char* ret = strstr(arr1, arr2);
if (ret == NULL)
{
printf("没找到\n");
}
else
{
printf("找到了:%s\n", ret);
}
return 0;
}
char * strtok ( char * str, const char * sep );
如果找到标记,则指向标记开头的指针。 否则为空指针。 当正在扫描的字符串中到达字符串结尾(即空字符)时,始终返回空指针。
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
#include
int main()
{
char arr[] = "[email protected] hehe";
char* p = "@. ";//分隔符的集合
char tmp[30] = { 0 };
strcpy(tmp, arr);//临时拷贝
//zpw\0bitedu\0tech\0
char* ret = NULL;
for (ret = strtok(tmp, p); ret != NULL; ret=strtok(NULL, p))
{
printf("%s\n", ret);
}
return 0;
}
char * strerror ( int errnum );
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
int main()
{
FILE* pf = fopen("test.txt", "r");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return 1;
}
fclose(pf);
pf = NULL;
return 0;
}
这里可以查看一下几个错误码
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
int main()
{
printf("%s\n", strerror(0));
printf("%s\n", strerror(1));
printf("%s\n", strerror(2));
printf("%s\n", strerror(3));
printf("%s\n", strerror(4));
printf("%s\n", strerror(5));
return 0;
}
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
#include
int main()
{
//打开文件失败的时候,会返回NULL
FILE* pf = fopen("test.txt", "r");//读文件
//0 "no error"
if (pf == NULL)
{
//printf("%s\n", strerror(errno));
perror("fopen");
return 1;
}
fclose(pf);//关闭文件
pf = NULL;
return 0;
}
函数
|
如果他的参数符合下列条件就返回真
|
iscntrl |
任何控制字符
|
isspace |
空白字符:空格 ‘ ’ ,换页 ‘\f’ ,换行 '\n' ,回车 ‘\r’ ,制表符 '\t' 或者垂直制表符 '\v'
|
isdigit |
十进制数字 0~9
|
isxdigit |
十六进制数字,包括所有十进制数字,小写字母 a~f ,大写字母 A~F
|
islower |
小写字母 a~z
|
isupper |
大写字母 A~Z
|
isalpha |
字母 a~z 或 A~Z
|
isalnum |
字母或者数字, a~z,A~Z,0~9
|
ispunct |
标点符号,任何不属于数字或者字母的图形字符(可打印)
|
isgraph |
任何图形字符
|
isprint |
任何可打印字符,包括图形字符和空白字符
|
int islower ( int c)
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
int main()
{
char ch = 'a';
char tb = 'A';
int ret = islower(ch);
printf("%d\n", ret);
printf("%c\n", ch);
ret = islower(tb);
printf("%d\n", ret);
printf("%c\n", tb);
return 0;
}
int isupper ( int c );
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
int main()
{
char arr[20] = { 0 };
scanf("%s", arr);
int i = 0;
while (arr[i] != '\0')
{
if (isupper(arr[i]))
{
arr[i] = tolower(arr[i]);
}
printf("%c ", arr[i]);
i++;
}
return 0;
}
其他的我就不介绍了,了解即可
void * memcpy ( void * destination , const void * source , size_t num );
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
int main()
{
int arr1[10] = { 1,2,3,4,5,6,7,8,9,10 };
//01 00 00 00 02 00 00 00 ...
int arr2[20] = { 0 };
memcpy(arr2, arr1, sizeof(arr1));
return 0;
}
注:如果source和destination有任何的重叠,复制的结果都是未定义的。
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
void* my_memcpy(void* dest, const void* src, size_t num)
{
void* ret = dest;//记录起始地址
assert(dest && src);
while (num--)//4 3 2 1
{
*(char*)dest = *(char*)src;
dest = (char*)dest + 1;
src = (char*)src + 1;
}
return ret;
}
int main()
{
int arr1[10] = { 1,2,3,4,5,6,7,8,9,10 };
//01 00 00 00 02 00 00 00 ...
int arr2[20] = { 0 };
my_memcpy(arr2, arr1, sizeof(arr1));
return 0;
}
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
int main()
{
int arr1[10] = { 1,2,3,4,5,6,7,8,9,10 };
memcpy(arr1+2, arr1, 20);
return 0;
}
注:如果你自己在vs2019中测试的时候发现,memcpy能拷贝有重叠的情况,别慌
只是在vs2019中成功了,并不保证所以的编译器都能成功,
C语言标准规定:当memcpy自己拷贝自己的时候,有重叠的部分,是拷贝不成功的
void * memmove ( void * destination , const void * source , size_t num );
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
int main()
{
int arr1[10] = { 1,2,3,4,5,6,7,8,9,10 };
// 1 2 1 2 3 4 5 8 9 10
memmove(arr1+2, arr1, 20);
return 0;
}
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
void* my_memmove(void* dest, const void* src, size_t num)
{
void* ret = dest;
//assert(dest && src);
if (dest < src)
{
//前->后
while (num--)
{
*(char*)dest = *(char*)src;
dest = (char*)dest + 1;
src = (char*)src + 1;
}
}
else
{
//后->前
while (num--)//19
{
*((char*)dest + num) = *((char*)src + num);
}
}
return ret;
}
int main()
{
int arr1[10] = { 1,2,3,4,5,6,7,8,9,10 };
// 1 2 1 2 3 4 5 8 9 10
my_memmove(arr1+2, arr1, 20);
return 0;
}
第一种情况 第二种情况 第三种情况
int memcmp ( const void * ptr1, const void * ptr2, size_t num );
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
//memcmp - 内存比较
int main()
{
float arr1[] = { 1.0, 2.0,3.0,4.0 };
float arr2[] = { 1.0, 3.0 };
int ret = memcmp(arr1, arr2, 4);
printf("%d\n", ret);
ret = memcmp(arr1, arr2, 6);
printf("%d\n", ret);
ret = memcmp(arr1, arr2, 8);
printf("%d\n", ret);
return 0;
}
void * memset ( void * ptr, int value, size_t num );
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
int main()
{
//40
int arr[10] = { 0 };//20
memset(arr, 1, 20);//以字节为单位设置内存的
//01 00 00 00
//01 01 01 01
return 0;
}