Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point).
原字符串必须以Appends a copy of the source string to the destination string. The terminating null character in destination is overwritten by the first character of source, and a null-character is included at the end of the new string formed by the concatenation of both in destination.结束。
Appends a copy of the source string to the destination string. The terminating null character in destination is overwritten by the first character of source, and a null-character is included at the end of the new string formed by the concatenation of both in destination.
This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ or until a terminating null-character is reached.
规定
第一个字符串大于第二个字符串,返回大于0的数字
第一个字符串等于第二个字符串,返回0.
第一个字符串小于第二个字符串,返回小于0的数字。
代码语言:javascript代码运行次数:0运行复制
#include <stdio.h>
#include <string.h>
int main()
{
char arr1[] = "abcd";
char arr2[] = "abc";
char arr3[] = "abc";
int ret = strcmp(arr1,arr2);
printf("arr1:arr2 %d\n",ret);
ret = strcmp(arr2,arr1);
printf("arr2:arr1 %d\n",ret);
ret = strcmp(arr2,arr3);
printf("arr2:arr3 %d\n",ret);
return 0;
}
//打印结果
/*
arr1:arr2 1
arr2:arr1 -1
arr2:arr3 0
*/
Copies the first num characters of source to destination. If the end of the source C string (which is signaled by a null-character) is found before num characters have been copied, destination is padded with zeros until a total of num characters have been written to it.
int strncmp(const char* str1,const char* str2,size_t num);
Compares up to num characters of the C string str1 to those of the C string str2.
This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ, until a terminating null-character is reached, or until num characters match in both strings, whichever happens first.
比较到出现另一个字符不一样或者一个字符串结束或者num个字符串全部比较完。
代码语言:javascript代码运行次数:0运行复制
#include <stdio.h>
#include <string.h>
int main()
{
char arr1[] = "abcd";
char arr2[] = "abc";
char arr3[] = "abc";
int ret = strncmp(arr1,arr2,3);
printf("arr1:arr2(3) %d\n",ret);
return 0;
}
//打印结果:arr1:arr2(3) 0
1.8 strstr
代码语言:javascript代码运行次数:0运行复制
char* strstr(const char* str1,const char* str2);
Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1.
//打印0~10所对应的错误信息
#include <stdio.h>
#include <string.h>
int main()
{
for(int i = 0;i<=10;++i)
{
printf("%d:%s\n",i,strerror(i));
}
return 0;
}
//打印结果
/*
0:No error
1:Operation not permitted
2:No such file or directory
3:No such process
4:Interrupted function call
5:Input/output error
6:No such device or address
7:Arg list too long
8:Exec format error
9:Bad file descriptor
10:No child processes
*/
字符分类函数
函数
如果它的参数符合下列条件就返回真
iscntrl
任何控制字符
isspace
空白字符:空格’‘,换页’\f’,回车’\r’,换行’\r’,制表符’\t’或者垂直制表符’\v’
isdigit
十进制数字0~9
isxdigit
十六进制数字,包括所有十进制数字,小写字母af,大写字母AF
islower
小写字母a~z
isupper
大写字母A~Z
isalpha
字母az或者AZ
isalnum
字母或者数字
ispunct
标点符号,任何不属于数字或者字母的图形字符(可打印)
isgraph
任何图形字符
isprint
任何可打印字符,包括图形字符和空白字符
字符转换函数
函数
功能
tolower
将大写字母转换为小写字母
toupper
将小写字母转换为大写指针
代码语言:javascript代码运行次数:0运行复制
//大写转小写
#include <stdio.h>
#include <ctype.h>
int main()
{
char str[] = "I Am A Student";
for(int i = 0;str[i];++i)
{
str[i] = tolower(str[i]);
}
return 0;
}
//打印结果:
//i am a student
发表评论