字符串处理函数
c库提供了许多字符串处理函数,定义在”string.h”中,其中常用的字符串处理函数有strlen(), strcat(), strcmp(), strncmp(), strcpy(), strncpy().另外还有sprintf()函数(定义在stdio.h中)
1. strlen()函数
strlen()主要是用来统计字符串的长度。
让我们先来看一个例子:
#include "string.h"
#include "stdio.h"
void fit(char *str, unsigned int size)
{
if(strlen(str) > size)
{
str[size] = '\0';
}
}
int main()
{
num = strlen("abcde");
printf("%d\n",num);
char msg[] = "this is a long string ";
puts(msg);
fit(msg, 12);
puts(msg);
puts(msg+15);
return 0;
}
运行结果如下:
5
[root@localhost]# ./strlen.o
this is a long string
this is a lo
strring
fit()函数是缩短字符串的函数,str[size]=’\0’作用是将第size个字符替换成’\0’,puts函数输出是遇到’\0’停止,并忽略其余字符,但是其它的字符还在缓冲区中。然后当puts(msg+15)时后面的字符串扔可被打印出来。
2. strcat()函数
strcat()是用于字符串拼接的函数,接受俩个入参。该函数把第2个字符串的备份附加到第1个字符串末尾,并把连接后形成的新字符串作为第一个字符串,第2个字符串不变。strcat()函数的类型是char *。strcat()函数返回一个参数,即拼接第二个字符串后的第一个字符串的地址。
#include <stdio.h>
#include <string.h>
#define SIZE 20
int main()
{
char flower[SIZE] = "yuejihua";
printf("%d\n", strlen(flower));
printf("%d\n", sizeof(flower));
char sun[] = "too beautiful";
strcat(flower, sun);
printf("%s\n", flower);
printf("%s\n", sun);
return 0;
}
执行结果如下:
8
20
yuejihuatoo beautiful
too beautiful
3. strncat()函数
strcat()函数无法检查第一个数组是否能容纳第2个字符串。如果分配的空间不够大,多出来的字符就会溢出到相邻存储单元就会出错。注意,可以先用strlen函数判断第1个数组的长度.注意:长度加1才够存放末尾字符’\0’。或者用strcat()函数,该函数第三个参数指定了最大添加字符数,
strncat(str1, str2, 13)
/*将str2字符串的内容附加给str1,在加到13个字符或者遇到空字符时停止。*/
4,strcmp()函数
该函数比较的是字符串的内容,不是字符串的地址,如果字符串参数相同就返回0,否则就返回非0值。
#include <stdio.h>
#include <string.h>
#define SIZE 30
#define BUGSIZE 13
int main()
{
char a[SIZE] = "hello";
char *b = "hello";
if ( strcmp(a, b) == 0)
{
printf("a equal b");
}
return 0;
}
执行结果如下:
a equal b
//当字符串a与b不等时返回的值
#nclude <stdio.h>
#include <string.h>
#define SIZE 30
#define BUGSIZE 13
int main()
{
char a[SIZE] = "helloA";
char *b = "hello";
int c = strcmp(a,b);
if ( c == 0)
{
printf("a equal b");
}
else
{
printf("%d\n",c);
}
return 0;
}
[root@localhost guogx]# ./strcmp
65
strcmp()比较所有的字符,不只是字母,根据的是字符的数值比较(通常是ASCII值)。实际上,char类型是整数类型,所以使用关系运算符来比较字符。
5,strncmp()函数
strncmp(str1, str2, 5)//比较str1和str2的前5个字符。
#include <stdio.h>
#include <string.h>
#define SIZE 6
int main()
{
const char *List[SIZE] = {"str123", "str234", "sshkjfa", "bandhja", "string", "str1ing"};
int count = 0;
int i;
for (i =0; i < SIZE; i++)
{
if (strncmp(List[i], "string", 3) == 0)
{
count++;
}
}
printf("%d\n", count);
return 0;
}
执行结果:
4
6,strcpy()
如果pstr1和pstr2都是指向字符串的指针,那么下面语句拷贝的是字符串的地址而不是字符串本身:
pstr1 = pstr2
如果想copy字符串本身,则需要使用strcpy函数,strcpy函数相当于字符串赋值运算符。
strcpy()函数的源码:
char *strcpy(char *dst, char *src)
{
assert(dst != NULL && src != NULL);
char *ret = dst;
while((*dst++ = *src++) != '\0');
return ret;
}
strcpy()函数接收俩个字符串指针作为参数,可以把指向源字符串的第2个指针声明为指针、数组名或字符串常量;而指向源字符串第一个指针应该指向一个数据对象(如:数组),且对该对象有足够的空间储存源字符串的副本。
strcpy()函数还有俩个重要的属性。第一、strcpy()的返回类型是char *,该函数返回的是第一个参数的值,即一个字符的地址。第二、第一1个参数不必指向数组的开始。这个属性可用于copy数组的一部分。
7,strncpy()函数
char *strncpy(char *dst, char *src, int n);
strcpy函数和strcat函数都有同样的问题,不能检查目标空间是否能容纳源字符串的副本,所以copy要用strncpy更安全,该函数第三个参数,指明copy的最大字符数。
8,sprintf()函数
sprintf()函数获取输入,并将其格式化输出,然后把格式化的字符串储存在format中。
#include<stdio.h>
#define MAX 20
int main()
{
char formal[2*MAX];
char first[MAX] = "today is mother's day";
char sec[MAX] = "OK";
int thr = 20180513;
sprintf(formal, "%s , %s-----, %d", first, sec, thr);
puts(formal);
return 0;
}
执行结果:
[root@localhost guogx]# ./strp.o
today is mother's da , OK-----, 20180513
其它字符串函数
char *strcpy(char *restrict s1, const char *restrict s2);
char *strncpy(char *restrict s1, const char *restrict s2, size_t n);
char *strcat(char *restrict s1, const char *restrict s2);
char *strcat(char *restrict s1, const char *restrict s2, size_t n);
int strcmp(const char *s1, const char *s2);
int strncmp(const char *s1, const char *s2, size_t n);
char *strstr(const char *s1, const char *s2);
//该函数返回指向s1字符串中s2字符串出现的首位置,如果没有找到则返回空指针。
size_t strlen(const char *s) //返回字符串的长度,不包括结尾符