对其函数的一些具体解释:
https://blog.csdn.net/Z_JUAN1/article/details/80422636
一、strchr
char str[] = "this is a sample string";
char *pch;
printf("looking for the 's' character in\"%s\"...\n",str);
pch = strchr(str, 's');
while (pch!=NULL)
{
printf("found at %d\n", pch - str + 1);
pch = strchr(pch + 1, 's');
}
二、strrchr
char str[] = "this is a sample string";
char *pch;
pch = strrchr(str, 's');
printf("last occurence of 's' found at %d\n",pch-str+1);
三、strpbrk
char str[] = "this is a sample string";
char key[] = "aeiou";
char *pch;
printf("vowels in '%s':", str);
pch= strpbrk(str, key);
while (pch!=NULL)
{
printf("%c", *pch);
pch = strpbrk(pch + 1, key);
}
四、strstr
char str[] = "this is a simple string";
char *pch;
pch = strstr(str, "simple");
strncpy(pch, "sample", 6);
printf("%s ", str);
五、strspn
int i;
char strtest[] = "129th";
char cest[] = "1234567890";
i = strspn(strtest, cest);
printf("the initial number has %d digits.\n", i);
六、strcspn
int i;
char strtest[] = "th123";
char cest[] = "1234567890";
i = strcspn(strtest, cest);
printf("the initial number has %d digits.\n", i);
七、strtok
char str[] = "- this, a sample string.";
char *pch;
printf("splitting string \" %s\"into tokens :\n", str);
pch = strtok(str, " ,.-");
while (pch != NULL)
{
printf("%s\n", pch);
pch = strtok(NULL, " ,.-");
}
八、strerror
#include<errno.h>
#pragma warning (disable:4996)
int main()
{
FILE *pFile;
pFile = fopen("unexist.ent", "r");
printf("%d->%s\n", errno, strerror(errno));
九、字符转化
#include<ctype.h>
#pragma warning (disable:4996)
int main()
{
int i = 0;
char str[] = "Test String.\n"; //放于数组可以被修改
char c;
while (str[i])
{
c = str[i];
if (isupper(c)) c=tolower(c);
putchar(c);
i++;
}
十、内存操作函数
1. memcpy(未考虑内存重叠)
struct{
char name[40];
int age;
}person,person_copy;
int main()
{
char myname[] = "Pierre de fermat";
memcpy(person.name, myname, strlen(myname + 1));
person.age = 46;
memcpy(&person_copy, &person, sizeof(person));
printf("%s %d \n", person_copy.name, person_copy.name);
2.memmove(考虑内存重叠)
3.memcmp(必须附上类型,方可比较) 其实也是通过ascii码比较
4.memchr
5.memset:初始化函数(按字节)
void *memset(void *s, int ch,size_tn);
memset:作用是在一段内存块中填充某个给定的值,它是对较大的结构体或数组进行清零操作的一种最快方法。
函数解释:将s中当前位置后面的n个字节 (typedef unsigned int size_t )用 ch 替换并返回 s 。
int a[10];
memset(a, 0, sizeof(a));
int a[10];
memset(a, 1, sizeof(a));
按字节进行初始化
将其转为二进制: