博客
关于我
【C语言】字符函数与字符串函数介绍及使用
阅读量:501 次
发布时间:2019-03-07

本文共 4336 字,大约阅读时间需要 14 分钟。

C语言字符串处理函数及其他相关函数教程

1. strlen

size_t strlen(const char* str);

功能说明:

strlen函数用于计算字符串的长度。
字符串在C语言中以 '\0' 结束,strlen函数返回 '\0' 前面的字符个数。

示例代码:

#include 
#include
int main() {
const char* str = "abcdef";
printf("%d\n", strlen(str));
return 0;
}

执行结果:

输出:6


2. strcpy

char* strcpy(char* destination, const char* source);

功能说明:

strcpy函数用于将源字符串拷贝到目标空间中。
目标空间必须足够大且可变,且源字符串必须以 '\0' 结束。

示例代码:

#include 
#include
int main() {
char str1[] = "Sample string";
char str2[40];
char str3[40];
strcpy(str2, str1);
strcpy(str3, "copy successful");
printf("str1: %s\nstr2: %s\nstr3: %s\n", str1, str2, str3);
return 0;
}

执行结果:

输出:
str1: Sample string
str2: Sample string
str3: copy successful


3. strcat

char* strcat(char* destination, const char* source);

功能说明:

将源字符串追加到目标字符串中。
源字符串必须以 '\0' 结束,目标空间必须足够大,能容纳源字符串的内容。

示例代码:

#include 
#include
int main() {
char str[80];
strcpy(str, "these ");
strcat(str, "strings ");
strcat(str, "are ");
strcat(str, "concatenated.");
puts(str);
return 0;
}

执行结果:

输出:these strings are concatenated.


4. strcmp

int strcmp(const char* str1, const char* str2);

功能说明:

比较两个字符串的大小关系。
返回值:

  • 大于0:str1大于str2
  • 等于0:str1等于str2
  • 小于0:str1小于str2

示例代码:

#include 
#include
int main() {
char key[] = "apple";
char buffer[80];
do {
printf("Guess my favorite fruit? ");
fflush(stdout);
scanf("%79s", buffer);
} while (strcmp(key, buffer) != 0);
puts("Correct answer!");
return 0;
}

执行结果:

输出:
Guess my favorite fruit? orange
Guess my favorite fruit? apple
Correct answer!


5. strstr

const char* strstr(const char* str1, const char* str2);

功能说明:

判断字符串str2是否是str1的子串。
如果是,返回str2在str1中首次出现的地址;否则返回NULL。

示例代码:

#include 
#include
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 sample string


6. strtok

char* strtok(char* str, const char* delimiters);

功能说明:

将字符串按照指定的分隔符分割成多个子串。
第一次调用时,str必须指向有效字符串,后续调用时,str可设为NULL,继续分割。

示例代码:

#include 
#include
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.


7. strerror

char* strerror(int errnum);

功能说明:

返回一个指针,该指针指向描述错误码errnum的静态字符串。
程序不得修改该字符串。

示例代码:

#include 
#include
#include
int main() {
FILE* pFile;
pFile = fopen("unexist.ent", "r");
if (pFile == NULL) {
printf("Error opening file unexist.ent: %s\n", strerror(errno));
}
return 0;
}

执行结果:

输出:打开文件不存在时出错:没有这样的文件或目录


字符分类函数

函数 描述
isdigit 判断字符是否为十进制数字(0-9)
islower 判断字符是否为小写字母(a-z)
isupper 判断字符是否为大写字母(A-Z)
isalpha 判断字符是否为字母(a-z或A-Z)
isalnum 判断字符是否为字母或数字(a-z, A-Z, 0-9)
isspace 判断字符是否为空白字符(space, tab等)

字符转换函数

函数 作用
tolower 将字符转换为小写字母
toupper 将字符转换为大写字母

10. memcpy

void* memcpy(void* destination, const void* source, size_t num);

功能说明:

从source位置开始,向目标地址destination拷贝num个字节的数据。
与strcpy不同, memcpy不会关注源字符串的 '\0'。

示例代码:

#include 
#include
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("person_copy: %s, %d \n", person_copy.name, person_copy.age);
return 0;
}

执行结果:

输出:person_copy: Pierre de Fermat, 46


11. memmove

void* memmove(void* destination, const void* source, size_t num);

功能说明:

与memcpy类似,但可以处理源内存块和目标内存块重叠的情况。

示例代码:

#include 
#include
int main() {
char str[] = "memmove can be very useful......";
memmove(str + 20, str + 15, 11);
puts(str);
return 0;
}

执行结果:

输出:memmove can be very very useful.


12. memcmp

void* memcmp(void* ptr1, const void* ptr2, size_t num);

功能说明:

与strcmp类似,但会比较整个num个字节,而不是在遇到 '\0' 时停止。
返回值:

  • < 0:ptr1中的第一个字节小于ptr2中的第一个字节
  • = 0:两个内存块内容相等
  • 0:ptr1中的第一个字节大于ptr2中的第一个字节

转载地址:http://ssacz.baihongyu.com/

你可能感兴趣的文章
Node.js的交互式解释器(REPL)
查看>>
Node.js的循环与异步问题
查看>>
Node.js高级编程:用Javascript构建可伸缩应用(1)1.1 介绍和安装-安装Node
查看>>
nodejs + socket.io 同时使用http 和 https
查看>>
NodeJS @kubernetes/client-node连接到kubernetes集群的方法
查看>>
NodeJS API简介
查看>>
Nodejs express 获取url参数,post参数的三种方式
查看>>
nodejs http小爬虫
查看>>
nodejs libararies
查看>>
nodejs npm常用命令
查看>>
nodejs npm常用命令
查看>>
Nodejs process.nextTick() 使用详解
查看>>
NodeJS yarn 或 npm如何切换淘宝或国外镜像源
查看>>
nodejs 中间件理解
查看>>
nodejs 创建HTTP服务器详解
查看>>
nodejs 发起 GET 请求示例和 POST 请求示例
查看>>
NodeJS 导入导出模块的方法( 代码演示 )
查看>>
nodejs 开发websocket 笔记
查看>>
nodejs 的 Buffer 详解
查看>>
NodeJS 的环境变量: 开发环境vs生产环境
查看>>