设为首页 加入收藏

TOP

C语言常用字符串函数操作详情(一)
2018-01-16 14:49:37 】 浏览:556
Tags:语言 常用 字符串 函数 操作 详情
1)字符串操作

strcpy(p, p1) 复制字符串

strncpy(p, p1, n) 复制指定长度字符串

strcat(p, p1) 附加字符串

strncat(p, p1, n) 附加指定长度字符串

strlen(p) 取字符串长度

strcmp(p, p1) 比较字符串

strcasecmp忽略大小写比较字符串

strncmp(p, p1, n) 比较指定长度字符串

strchr(p, c) 在字符串中查找指定字符

strrchr(p, c) 在字符串中反向查找

strstr(p, p1) 查找字符串

strpbrk(p, p1) 以目标字符串的所有字符作为集合,在当前字符串查找该集合的任一元素

strspn(p, p1) 以目标字符串的所有字符作为集合,在当前字符串查找不属于该集合的任一元素的偏移

strcspn(p, p1) 以目标字符串的所有字符作为集合,在当前字符串查找属于该集合的任一元素的偏移

* 具有指定长度的字符串处理函数在已处理的字符串之后填补零结尾符

2)字符串到数值类型的转换

strtod(p, ppend) 从字符串 p 中转换 double 类型数值,并将后续的字符串指针存储到 ppend 指向的 char* 类型存储。

strtol(p, ppend, base) 从字符串 p 中转换 long 类型整型数值,base 显式设置转换的整型进制,设置为 0 以根据特定格式判断所用进制,0x, 0X 前缀以解释为十六进制格式整型,0 前缀以解释为八进制格式整型

atoi(p) 字符串转换到 int 整型

atof(p) 字符串转换到 double 符点数

atol(p) 字符串转换到 long 整型

3)字符检查

isalpha() 检查是否为字母字符

isupper() 检查是否为大写字母字符

islower() 检查是否为小写字母字符

isdigit() 检查是否为数字

isxdigit() 检查是否为十六进制数字表示的有效字符

isspace() 检查是否为空格类型字符

iscntrl() 检查是否为控制字符

ispunct() 检查是否为标点符号

isalnum() 检查是否为字母和数字

isprint() 检查是否是可打印字符

isgraph() 检查是否是图形字符,等效于 isalnum() | ispunct()

4)函数原型

原型:strcpy(char destination[], const char source[]);

功能:将字符串source拷贝到字符串destination中

例程:

#include

#include

void main(void)

{

char str1[10] = { "TsinghuaOK"};

char str2[10] = { "Computer"};

cout <

}

运行结果是:Computer

第二个字符串将覆盖掉第一个字符串的所有内容!

注意:在定义数组时,字符数组1的字符串长度必须大于或等于字符串2的字符串长度。不能用赋值语句将一个字符串常量或字符数组直接赋给一个字符数组。所有字符串处理函数都包含在头文件string.h中。

strncpy(char destination[], const char source[], int numchars);

strncpy:将字符串source中前numchars个字符拷贝到字符串destination中。

strncpy函数应用举例

原型:strncpy(char destination[], const char source[], int numchars);

功能:将字符串source中前numchars个字符拷贝到字符串destination中

例程:

#include

#include

void main(void)

{

char str1[10] = { "Tsinghua "};

char str2[10] = { "Computer"};

cout <

}

运行结果:Comnghua

注意:字符串source中前numchars个字符将覆盖掉字符串destination中前numchars个字符!

原型:strcat(char target[], const char source[]);

功能:将字符串source接到字符串target的后面

例程:

#include

#include

void main(void)

{

char str1[] = { "Tsinghua "};

char str2[] = { "Computer"};

cout <

}

运行结果:Tsinghua Computer

注意:在定义字符数组1的长度时应该考虑字符数组2的长度,因为连接后新字符串的长度为两个字符串长度之和。进行字符串连接后,字符串1的结尾符将自动被去掉,在结尾串末尾保留新字符串后面一个结尾符。

原型:strncat(char target[], const char source[], int numchars);

功能:将字符串source的前numchars个字符接到字符串target的后面

例程:

#include

#include

void main(void)

{

char str1[] = { "Tsinghua "};

char str2[] = { "Computer"};

cout <

}

运行结果:Tsinghua Com

原型:int strcmp(const char firststring[], const char secondstring);

功能:比较两个字符串firststring和secondstring

例程:

#include

#include

void main(void)

{

char buf1[] = "aaa";

char buf2[] = "bbb";

char buf3[] = "ccc";

int ptr;

ptr = strcmp(buf2,buf1);

if(ptr > 0)

cout <<"Buffer 2 is greater than buffer 1"<

else

cout <<"Buffer 2 is less than buffer 1"<

ptr = strcmp(buf2,buf3);

if(ptr > 0)

cout <<"Buffer 2 is greater than buffer 3"<

else

cout <<"Buffer 2 is less than buffer 3"<

}

运行结果是:Buffer 2 is less than buffer 1

Buffer 2 is greater than buffer 3

原型:strlen( const char string[] );

功能:统计字符串string中字符的个数

例程:

#include

#include

void main(void)

{

char str[100];

cout <<"请输入一个字符串:";

cin >

首页 上一页 1 2 3 4 下一页 尾页 1/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇C语言中易错函数实例讲解 下一篇C语言常用关键字函数总结

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目