C语言常用字符串函数总结

时间:2021-08-15 05:54:05
ANSI C中有20多个用于处理字符串的函数:

注意:const 形参使用了const限定符,表示该函数不会改变传入的字符串。因为源字符串是不能更改的。

strlen函数:

函数原型:unsigned int strlen(const char*)

用于统计字符串的长度。举例如下

 void fit(char *,unsigned int);

 int main(void)
{
char mesg [] = "Things should be as simple as possible,""but not simpler."; puts(mesg);
fit(mesg, );
puts(mesg);
puts("Let's look at some more of the string.");
puts(mesg + ); //我们在38位置(空字符\0)后的39位置开始打印缓冲区余下的字符串。 return ;
} void fit(char *string, unsigned int size) //利用strlen函数,设计一个函数可以缩短字符串的长度。
{
if(strlen(string)>size)
string[size] = '\0';
}

strcat()函数:

函数原型:char *strcat(char *strDest, const char *strSrc)

接受两个字符串作为参数,把第2个字符串的备份附加在第1个字符串的末尾。并把拼接后形成的新字符串作为第1个字符串。第2个字符串不变。

 #include <stdio.h>
#include <string.h>
#define SIZE 80 char * s_gets(char * st, int n); int main()
{
char flower[SIZE];
char addon [] = "s smell like old shoes."; puts("What is your favorite flower?");
if(s_gets(flower,SIZE))
{
strcat(flower,addon); //使用了strcat函数进行字符串拼接
puts(flower);
puts(addon);
}
else
puts("End of file encountered!");
puts("bye"); return ;
} char * s_gets(char * st, int n)
{
char * ret_val;
int i=; ret_val = fgets(st, n, stdin); //读取成功,返回一个指针,指向输入字符串的首字符;
if(ret_val)
{
while(st[i]!='\n' && st[i]!='\0')
i++;
if(st[i] =='\n') //fgets会把换行符也吃进来了,fgets会在末尾自动加上\0;
st[i]='\0';
else
while(getchar() != '\n')
continue;
}
return ret_val;
}

strncat()函数:

strcat()函数有个缺点就是无法检查第1个数组是否能容纳第2个字符串。如果给第1个数组的空间不够大,多出来的字符溢出到相邻存储单元时就会出问题。

函数原型:extern char *strncat(char *dest,char *src,int n);

参数说明:第3个参数指定了最大添加字符数;

这段代码会拼接两个字符,检查第1个数组的大小,重点是确定最大能添加多少字符数;

 #include<stdio.h>
#include<string.h> #define SIZE 30
#define BUGSIZE 13 char * s_gets(char * st,int n); int main(void)
{
char flower[SIZE];
char addon [] = "s smell like old shoes.";
char bug[BUGSIZE];
int available; puts("What is your favorite flower?");
s_gets(flower,SIZE);
if((strlen(addon)+strlen(flower)+)<=SIZE) //important
strcat(flower,addon);
puts(flower);
puts("What is your favorite bug?");
s_gets(bug,BUGSIZE);
available= BUGSIZE -strlen(bug)-;
strncat(bug,addon,available);
puts(bug);
return ;
} char * s_gets(char * st, int n)
{
char * ret_val;
int i=; ret_val = fgets(st, n, stdin); //读取成功,返回一个指针,指向输入字符串的首字符;
if(ret_val)
{
while(st[i]!='\n' && st[i]!='\0')
i++;
if(st[i] =='\n') //fgets会把换行符也吃进来了,fgets会在末尾自动加上\0;
st[i]='\0';
else //其实是'\0'
while(getchar() != '\n') //会把缓冲区后续的字符都清空
continue;
}
return ret_val;
}

我们发现strcat()也会造成缓冲区溢出。但是它没有像gets()一样被废除。gets()的安全隐患来自使用程序的人。而strcat()的安全隐患来自粗心的程序员。我们无法控制用户会进行什么操作,但是可以控制程序做什么。因此C语言相信程序员。程序员也有责任保证strcat()使用的安全。

缓冲区溢出漏洞:程序员必须保证strcat的第一个参数有足够的空间。编译器无法报错是因为,这个函数的参数是指针类型,函数中也只是通过指针来读写这些内存的。函数根本不知道第一个参数所指的内存空间到底够不够大,函数本身不会对此进行检查。函数的大致行为是找到第一个参数所指的内存中字符串结尾的位置,然后从此处开始写入第二个参数的字符,直到写完。如果向第一个参数写入过多的字符,有可能会引起问题,也有可能不会。这取决于内存空间后面的内存是否可用来读写。万一覆盖了内存空间重要数据,就会引起错误。所以这是严重的安全隐患。

strcmp()函数

函数原型:int strcmp(const char *str1,const char *str2);

strcmp()函数返回的具体值不重要,我们只在意该值是0还是非0;比较两个字符串是否相等;我们关注的是字符串是否相等;

如果真要关心返回值的话,要理解比较的机制:其实是ASCII码值的比较;大写的字母ASCII值比小写的字母小;

比较的是:第一个字符串,相对第二个字符串的大小

str1<str2 返回负值;(ASCII的差值)

str1=str2 等于0;

str1>str2 返回正值;(ASCII的差值)

 //检查程序是否要停止读取输入

 #include <stdio.h>
#include <string.h> #define SIZE 80
#define LIM 10
#define STOP "quit" char * s_gets(char *st, int n); int main(void)
{
char input[LIM][SIZE];
int ct =; printf("Enter up to %d lines(type quit to quit):\n",LIM);
while(ct<LIM && s_gets(input[ct],SIZE)!=NULL && strcmp(input[ct],STOP)!=)
ct++;
printf("%d strings entered\n",ct); return ;
} char * s_gets(char * st, int n)
{
char * ret_val;
int i=; ret_val = fgets(st, n, stdin); //读取成功,返回一个指针,指向输入字符串的首字符;
if(ret_val)
{
while(st[i]!='\n' && st[i]!='\0')
i++;
if(st[i] =='\n') //fgets会把换行符也吃进来了,fgets会在末尾自动加上\0;
st[i]='\0';
else //其实是'\0'
while(getchar() != '\n') //会把缓冲区后续的字符都清空
continue;
}
return ret_val;
}

strncmp()函数:

函数原型:extern int strcmp(char *str1,char * str2,int n)

参数说明:str1为第一个要比较的字符串,str2为第二个要比较的字符串,n为指定的str1与str2的比较的字符数。

函数功能:比较字符串str1和str2的前n个字符。

返回说明:返回整数值:当str1<str2时,返回值<0; 当str1=str2时,返回值=0; 当str1>str2时,返回值>0。

 //比较前五个字符是否相等
#include <stdio.h>
#include <string.h> #define LISTSIZE 6 int main()
{
const char *list[LISTSIZE]=
{
"astronomy","astounding",
"astrophysics","ostracize",
"asterism","astrophobia"
};
int count =;
int i; for(i=;i<LISTSIZE;i++)
{
if(strncmp(list[i],"astro",)==)
printf("Found:%s\n",list[i]);
count++;
}
printf("The list contained %d words beginning" " with astro.\n",count); return ;
}

strcpy()函数:

函数原型:char *strcpy(char *strDest, const char *strSrc)

这个函数相当于字符串的赋值运算,拷贝字符串;

 #include <stdio.h>
#include <string.h>
#define SIZE 40
#define LIM 5
char * s_gets(char * st, int n); int main(void)
{
char qwords[LIM][SIZE];
char temp[SIZE];
int i = ; printf("Enter %d words beginning with q:\n",LIM);
while(i<LIM && s_gets(temp,SIZE))
{
if(temp[]!='q')
printf("%s doesn't begin with q!\n",temp);
else
{
strcpy(qwords[i],temp);
i++;
}
}
puts("Here are the words accepted:");
for(i=;i<LIM;i++)
{
puts(qwords[i]);
}
return ;
}

声明数组将分配储存数据的空间,声明指针只分配储存一个地址的空间;

使用strcpy函数的时候,程序员有责任保证目标字符串有足够的空间储存源字符串的副本;

strcpy的一些属性:其返回类型是char *,第一,该函数返回的是第1个参数的值,即一个字符的地址;第二,第1个参数不必指向数组的开始。这个属性可用于拷贝数组的一部分。代码如下:

 #include <stdio.h>
#include <string.h>
#define WORDS "beast"
#define SIZE 40 int main(void)
{
const char * orig = WORDS;
char copy[SIZE]= "Be the best that you can be.";
char *ps; puts(orig);
puts(copy);
ps = strcpy(copy+,orig);
puts(copy);
puts(ps); return ;
}

注意:源字符串也会把空字符也拷贝在内。

strncpy()函数:更谨慎的选择

函数原型:char *strncpy(char *dest, char *src, int n);

strcpy()和strcat()都有同样的问题,就是不能检查目标空间能否容纳源字符串的副本。拷贝字符串用strncpy()更安全。第3个参数指明可拷贝的最大字符数

 //还是输入五个q开头的单词,但是对单词输入的长度有限制
#include <stdio.h>
#include <string.h>
#define SIZE 40
#define TARGSIZE 7
#define LIM 5 char * s_gets(char * st, int n); int main(void)
{
char qwords[LIM][TARGSIZE];
char temp[SIZE];
int i=; printf("Enter %d words beginning with q:\n",LIM);
while(i<LIM && s_gets(temp,SIZE))
{
if(temp[]!='q')
printf("%s doesn't begin with q!\n",temp);
else
{
strncpy(qwords[i],temp,TARGSIZE-);
qwords[i][TARGSIZE-] ='\0';
i++;
}
}
puts("Here are the words accepted:");
for (i=;i<LIM;i++)
{
puts(qwords[i]); //puts末尾自动加上换行符
} return ;
} char * s_gets(char * st, int n)
{
char * ret_val;
int i=; ret_val = fgets(st, n, stdin); //读取成功,返回一个指针,指向输入字符串的首字符;
if(ret_val)
{
while(st[i]!='\n' && st[i]!='\0')
i++;
if(st[i] =='\n') //fgets会把换行符也吃进来了,fgets会在末尾自动加上\0;
st[i]='\0';
else //其实是'\0'
while(getchar() != '\n') //会把缓冲区后续的字符都清空
continue;
}
return ret_val;
}

strncpy(target,source,n)把source中的n个字符或空字符之前的字符(先满足哪个条件就执行哪个)拷贝到target中。

如果source中的字符数小于n,则拷贝整个字符串,包括空字符。

但是有一种情况就是万一没有把末尾的空字符拷贝进去的话,就不是存的字符串了。所以一般要把n设置成比目标数组的大小少1,然后把数组最后一个元素设置为空字符。

代码如下:

 strncpy(qwords[i],temp,TARGSIZE-);
qwords[i][TARGSIZE-] ='\0';

这样做确保是一个字符串。

sprintf()函数

函数原型:int sprintf( char *buffer, const char *format [, argument] … );

注意:它函数声明在stdio.h中,而不是在string.h中;该函数和printf()有点像。但是它是把数据写入字符串中,而不是打印在显示器上。

第一个参数是目标字符串的地址。其余参数和printf()相同,即格式字符串和待写入项的列表

sprintf 是个变参函数,使用sprintf 对于写入buffer的字符数是没有限制的,这就存在了buffer溢出的可能性。

 //格式化字符串
#include <stdio.h>
#define MAX 20
char * s_gets(char * st, int n); int main(void)
{
char first[MAX];
char last[MAX];
char formal[*MAX+];
double prize; puts("Enter your first name:");
s_gets(first,MAX);
puts("Enter your last name:");
s_gets(last,MAX);
puts("Enter your prize money:");
scanf("%lf",&prize);
sprintf(formal,"%s, %-19s: $%6.2f\n",last,first,prize);
puts(formal); return ;
} char * s_gets(char * st, int n)
{
char * ret_val;
int i=; ret_val = fgets(st, n, stdin); //读取成功,返回一个指针,指向输入字符串的首字符;
if(ret_val)
{
while(st[i]!='\n' && st[i]!='\0')
i++;
if(st[i] =='\n') //fgets会把换行符也吃进来了,fgets会在末尾自动加上\0;
st[i]='\0';
else //其实是'\0'
while(getchar() != '\n') //会把缓冲区后续的字符都清空
continue;
}
return ret_val;
}

strchr函数:

函数原型:char *strchr(const char *s, int c)

如果s字符串中包含c字符,该函数返回指向s字符串首位置(指向字符)的指针;如果在字符串中未找到c字符,该函数则返回空指针;

strrchr函数:

函数原型:char *strrchr(const char * s,int c)

该函数返回s字符串中c字符的最后一次出现的位置(末尾的空字符也是字符串的一部分,所以在查找范围内)。如果未找到c字符,则返回空指针;

strstr函数:

函数原型:char *strstr(const char * s1, const char * s2);

返回值是指向s1字符串中s2字符串出现的首位置。如果在s1中没有找到s2,则返回空指针。

strpbrk函数:

函数原型:char *strpbrk(const char * s1, const char * s2);

如果s1字符中包含s2字符串中的任意字符,该函数返回指向s1字符串首位置的指针;如果在s1字符串中未找到任何s2字符串中的字符,则返回空指针。

size_t strlen(const char * s)

该函数返回s字符串中的字符数,不包括末尾的空字符

size_t类型是sizeof运算符返回的类型。

C规定sizeof运算符返回一个整数类型,但是并未指定是哪种整数类型

所以size_t在一个系统中可以是unsigned int,而在另一个系统中可以是unsigned long。

string.h头文件针对特定系统定义了size_t。这样就可以跨平台了,屏蔽平台之间的差异性。

//fgets()读入一行输入时,在目标字符串的末尾添加换行符

//处理末尾换行符的方法之一是循环检测换行符,然后替换成\0

 char * s_gets(char * st, int n)
{
char * ret_val;
int i=; ret_val = fgets(st, n, stdin); //读取成功,返回一个指针,指向输入字符串的首字符;
if(ret_val)
{
while(st[i]!='\n' && st[i]!='\0')
i++;
if(st[i] =='\n') //fgets会把换行符也吃进来了,fgets会在末尾自动加上\0;
st[i]='\0';
else //其实是'\0'
while(getchar() != '\n') //会把缓冲区后续的字符都清空
continue;
}
return ret_val;
}

有其他办法,使用strchr()代替s_gets():

 #include <stdio.h>

 char line[];
char * find; fgets(line, , stdin);
find = strchr(line, '\n');
if(find)
* find ='\0';

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

注意点:

 while(* string)

2 while(*string != '\0')

许多程序员会在while循环条件中使用第一种的测试条件;string指向空字符时,*string的值是0,即测试条件为假,while循环结束。作为C语言程序员应该熟悉方法,第二种的测试条件没有第一种简洁。

const char * string和const char string[]的区别:

从技术方面讲,两者等效且都有效;

使用方括号是为了提醒用户,实际处理的参数是数组;

如果要处理字符串,使用指针形式的话,实际参数可以是数组名,双引号括起来的字符串或声明为char *类型的变量。这种写法是为了提醒用户,实际参数不一定是数组;

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

演示1:嵌套函数的调用

 #include <stdio.h>

 void put1(const char *);
int put2(const char *); int main(void)
{
put1("If I'd as much money");
put1(" as I could spend,\n");
printf("I count %d characters.\n",
put2("I never would cry old chairs to mend.")); return ;
} void put1(const char * string)
{
while(*string)
putchar(* string++); } int put2(const char * string)
{
int count = ;
while(*string)
{
putchar(*string++);
count++;
}
putchar('\n'); return(count);
}

分析:使用printf()打印put2的值,但是为了获得put2的返回值,计算机必须先执行put2(),因此执行put2()的过程中,打印了put2中的字符串。