数据结构常用的字符串函数(中英双释)

头文件:string.h

1.strchr

const char * strchr ( const char * str, int character );
Locate first occurrence of character in string

str

C string.

character

Character to be located.

Return Value

A pointer to the first occurrence of  character  in  str .

If the character is not found, the function returns a null pointer.

2.strlen
function
<cstring>

strlen

size_t strlen ( const char * str );
Get string length

Return Value

Returns the length of the C string str.
3.strcmp
char * strcpy ( char * destination, const char * source );
Copy string

Parameters

destination

Pointer to the destination array where the content is to be copied.

source

C string to be copied.

Return Value

destination  is returned.
4.strcat
char * strcat ( char * destination, const char * source );
Concatenate strings    //追加字符串

Parameters

destination

Pointer to the destination array, which should contain a C string, and be large enough to contain the concatenated resulting string.

source

C string to be appended. This should not overlap destination.

Return Value

destination  is returned.
注:
源字符串必须以 '\0' 结束。
目标空间必须有足够的大,能容纳下源字符串的内容。
目标空间必须可修改。
5.strcmp
int strcmp ( const char * str1, const char * str2 );
Compare two strings

Parameters

str1

C string to be compared.

str2

C string to be compared.

Return Value

Returns an integral value indicating the relationship between the strings:
return value
indicates
<0
the first character that does not match has a lower value in  ptr1 than in  ptr2
0
the contents of both strings are equal
>0
the first character that does not match has a greater value in  ptr1 than in  ptr2
6.strncpy  :长度限制为num字节
char * strncpy ( char * destination, const char * source, size_t num );
Copy characters from string
7.strncat  :追加长度为n字节
char * strncat ( char * destination , const char * source , size_t num );
8.strncmp  :比较个数为n字节
9.strstr :查找子串
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.
注意:返回的是在str1中的指针
10.strtok:截断字符串、存在记忆功能
char * strtok ( char * str , const char * sep );
1.sep 参数是个字符串,定义了用作分隔符的字符集合
2.第一个参数指定一个字符串,它包含了 0 个或者多个由 sep 字符串中一个或者多个分隔符分割的标 记。
3. strtok 函数找到 str 中的下一个标记,并将其用 \0 结尾,返回一个指向这个标记的指针。(注:
strtok 函数会改变被操作的字符串,所以在使用 strtok 函数切分的字符串一般都是   !!! 临时拷贝 !!!   的内容 并且可修改。)
4. strtok 函数的第一个参数不为 NULL ,函数将找到 str 中第一个标记, strtok 函数将保存它在字符串 中的位置,作为一个标记。
5 . strtok 函数的第一个参数为 NULL ,函数将在同一个字符串中被保存的位置开始,查找下一个标 记。
11.strerror
char * strerror ( int errnum );  
返回错误码所对应的错误信息。(只是返回,不打印)
常结合errno这个全局变量使用(头文件为errno.h)
errno记录着错误码
12.perror:打印错误码对应的错误信息
#include <stdio.h>
#include <errno.h>
int main() {
    FILE* file = fopen( "none.txt", "r");
    if (file == NULL) {
        perror( "Error");   //打印为:Error: No such file or directory       //自动换行
        perror( "Error fopen");   //打印为:Error fopen: No such file or directory
        return 1;
    }
    return 0;
}
  • 6
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值