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

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

文章目录

1. strlen

size_t strlen(const char* str);

  • 计算字符串长度
  • 字符串以 ‘\0’ 作为结束标志,函数返回字符串中 ‘\0’ 前面出现的字符个数
  • size_t 是无符号的
#include 
int main(){ const char* str = "abcdef"; printf("%d\n", strlen(str)); return 0;}

执行结果

6

 

2. strcpy

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

  • 拷贝字符串
  • 字符串以 '\0’结束,会将源字符串中的 ‘\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

  • 追加字符串
  • 源字符串必须 ‘\0’ 结束
  • 目标空间足够大,能容纳源字符串的内容
// 函数:char* strcat(char* destination, const char* source_);#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的数字
  • 第一个字符串等于第二个,返回0
  • 的第一个字符小于第二个字符串,返回小于0的数字
#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 favourite fruit? orange

Guess my favourite fruit? apple
Correct answer!

 

5. strstr

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

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

  • 判断是否包含
  • strstr(str1,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 指向欲分割的字符串,参数delim 则以什么分割(当发现参数delimiters该出字符被替换为 \0)。
  • 在第一次调用时,strtok()必需给予参数s 字符串,往后的调用则将参数s 设置成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;}

 

7. strerror

char * strerror(int 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;}

执行结果

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

 

8. 字符分类函数

函数 符合下列条件就为真
isdigit 十进制0-9
islower 小写字母a-z
isupper 大写字母A-Z
isalpha 字母a-z或A-Z

 

9. 字符转换函数

函数 作用
tolower 转换成小写字母
toupper 转换成大写字母

 

10. memcpy

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

  • 从source的位置开始向后拷贝num个字节的数据到destination
  • 遇到 ‘\0’ 不会停下来
#include 
#include
struct { char name[40]; int age;} person, person_copy;int main (){ char myname[] = "Pierre de Fermat"; /* using memcpy to copy string: */ memcpy ( person.name, myname, strlen(myname)+1 ); person.age = 46; /* using memcpy to copy structure: */ 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的区别就是memmove可以处理源内存和目标内存块是可以重叠的
#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 * memcpy ( void * destination, const void * source, size_t num );

  • 与strcmp不同,该函数在找到空字符后不会停止比较。
  • 返回值
返回值 表示
< 0 两个内存块中不匹配,第一个字节在ptr1中的值比在ptr2中的值低
= 0 两个内存块的内容相等
> 0 两个内存块中不匹配,第一个字节在ptr1中的值大于在ptr2中的值

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

你可能感兴趣的文章
mysql主从配置
查看>>
MySQL之2003-Can‘t connect to MySQL server on ‘localhost‘(10038)的解决办法
查看>>
MySQL之CRUD
查看>>
MySQL之DML
查看>>
Mysql之IN 和 Exists 用法
查看>>
MYSQL之REPLACE INTO和INSERT … ON DUPLICATE KEY UPDATE用法
查看>>
MySQL之SQL语句优化步骤
查看>>
MYSQL之union和order by分析([Err] 1221 - Incorrect usage of UNION and ORDER BY)
查看>>
Mysql之主从复制
查看>>