strcpy 字符串拷贝
函数原型:char * strcpy(char * destination, const char *source);
- Copies the C string pointed by source into the array pointed by destination, including terminating null character (and stopping at that point).
- 源字符串必须以’\0’结束。
- 会将源字符串中的’\0’拷贝到目标空间。
- 目标空间必须足够大,以确保能存放源字符串。
- 目标空间必须可变,即为数组。
模拟实现strcpy函数,如下:
char* my_strcpy(char * destination, const char * source)
{
char* p = destinaton;
assert(destination && source);
while(*destination++ = *source++)
{
;
}
return p;
}
strcat 字符串追加
函数原型:char* strcat(char* destination, const char* source);
- Appends a copy the source string to the destination string. The terminating null in destination is overwritten by the first character of source, and a null-character is at the end of the new string formed by the concatenation of both in destination.
- 源字符串必须以’\0’结束
- 目标空间必须足够大,以确保能存放源字符串。
- 目标空间必须可变,即为数组。
使用方法如下:
int main()
{
char arr[20] = "hello ";
strcat(arr, "world");
printf("%s\n", arr);
}
打印结果如下:
hello world
模拟实现strcat, 如下所示:
char * my_strcat(char* dest, const char* src)
{
assert(dest && src);
char* p = dest;
while(*dest != '\0')
{
dest++;
}
while(*dest++ = *src++)
{
;
}
return p;
}
strcmp 字符串大小比较
函数原型:int strcmp(const char* str1, const char* str2);
- This function starts comparing the first character of each string. If they are equal to other, it continues with the following pairs until the characters differ or until a terminating null-character is reached.
- 第一个字符串大于第二个字符串,则返回大于0的数字。
- 第一个字符串等于第二个字符串,则返回0。
- 第一个字符串校园第二个字符串,则返回小于0的数字。
int main()
{
char arr1[20] = "zhangsan";
char arr2[] = "zhangsanfeng";
int ret = strcmp(arr1, arr2);
if (ret < 0)
{
printf("<\n");
}
else if (ret == 0)
{
printf("==\n");
}
else
{
printf(">\n");
}
}
模拟strcmp实现,如下:
int my_strcmp(const char* str1, const char*str2)
{
assert(str1 && str2);
while(*str1 == *str2)
{
if (*str1 == '\0')
return 0; //字符串相等
str1++;
str2++;
}
// if (*str1 > *str2)
// return 1;
// else
// return -1;
return (*str1 - *str2);
}
#strncpy 拷贝指定数量的字符
函数原型:char * strncpy(char * destination, const char * source, size_t num);
- 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 writeen to it.
int main()
{
char arr1[20] = "abcdef";
char arr2[] = "hello world";
char arr3[] = "hello";
strncpy(arr1, arr2, 5);
printf("%s\n", arr1);
strncpy(arr1, arr3, 8); // arr3字符串串长度小于8,则不够的用\0补上
printf("%s\n", arr1);
}
打印结果如下:
hellof
hello
strncat 追加指定数量的字符串
函数原型:char * strncat(char * destination, const char * source , size_t num);
Appends the first num characters of source to destination, plus a terminating null-character.
If the length of the C string in source is less than num, only the content up to the terminating null-character is copied.
int main()
{
char arr1[20] = "hello ";
char arr2[] = "world";
strncat(arr1, arr2, 3); //在追加的末尾加上'\0'
printf("%s\n", arr1);
}
打印结果:
hello wor
strstr 查找字符串
函数原型:const char * strstr(const char * str1, const char * str2);
返回指向 str1 中第一次出现 str2 的指针,如果 str2 不是 str1 的一部分,则返回 null 指针。
匹配过程不包括终止 null 字符,但它会在此处停止。
参数:str1 要扫描的字符串
str2 要搜索匹配的字符串
int main ()
{
char str[] ="This is a simple string";
char * pch;
pch = strstr (str,"simple");
if (pch != NULL)
strncpy (pch,"sample",6);
puts (str);
return 0;
}
输出结果:
This is a simple string
strtok 字符分割
函数原型:char * strtok(char * str, const char *sep);
- sep参数是个字符串,定义了分隔符的字符集合
- 第一个参数指定一个字符串,它包含了0个或者多个由sep字符串中一个或者多个分隔符的标记。
- strtok函数找到str中的下一个标记,并将其\0结尾,返回一个指向这个标记的指针(注:strtok函数会改变被操作的字符串,所以在使用strtok函数切分的字符串一般都是临时拷贝的内容,并且可以修改)
- strtok的第一个参数不为NULL,函数将找到第一个标记,strtok将保存它在字符串中的位置。
- strtok函数的第一个参数为NULL,函数将在同一个字符串中被保存的位置开始,查找下一个标记。
- 如果字符串不存在更多标记,则返回NULL指针。
int main ()
{
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, " ,.-");
}
return 0;
}
输出
Splitting string "- This, a sample string." into tokens:
This
a
sample
string
strerror 错误信息输出
函数原型:char * strerror(int errnum);
返回错误码所对应的信息
int main()
{
FILE* pf = fopen("test.txt", "r");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return -1;
}
return 0;
}
输出
No such file or directory