【C语言】str 系列字符串操作函数
strlen 函数
strlen 函数用于统计字符串长度
size_t strlen(const char *_Str);
strcpy 函数
strcpy 函数用于将某个字符串复制到字符数组中
char *strcpy(char *_Dest,const char *_Source);
strcat 函数
strcat 函数用于将两个字符串拼接到一起
char *strcat(char *_Dest,const char *_Source);
strcmp 函数
strcmp 函数用于比较两个字符串的大小
int strcmp(const char *_Str1,const char *_Str2);
举例
#include <stdio.h>
#include <string.h> //添加该头文件才可使用 str 系列字符串操作函数
int main() {
int len;
char c[20];
char d[20] = "world";
gets(c);
puts(c);
len = strlen(c); //统计字符串的长度,不包括'\0'
printf("len=%d\n",len);
strcat(c,d); //把d的字符串拼接到c中
puts(c);
strcpy(d,c); //把c中的字符串复制到d中
puts(d);
//c大于"how",返回值是正值;相等是0;c小于"how",返回值是负值
printf("%d\n",strcmp(c,"how"));
return 0;
}
strrev 函数
strrev 函数将字符串逆置
char *strrev(char *_Str)
strrev 函数不会生成新字符串,而是修改原有字符串。因此它只能逆置字符数组,而不能逆置字符串指针指向的字符串,因为字符串指针指向的是字符串常量,常量不能被修改。
本文来自博客园,作者:hzyuan,转载请注明原文链接:https://www.cnblogs.com/hzyuan/p/17960530