字符函数及字符串函数详解

本章重点

求字符串长度
strlen
长度不受限制的字符串函数
strcpy
strcat
strcmp
长度受限制的字符串函数介绍
strncpy
strncat
strncmp
字符串查找
strstr
strtok
错误信息报告
strerror
字符操作
内存操作函数
memcpy
omemmove
memset

strlen

size_t strlen(constchar *str);

1.字符串已经\0'作为结束标志,strlen函数返回的是在字符串中\0前面出现的字符个数(不包含\0’)。

2.参数指向的字符串必须要以'\0'结束。

3.注意函数的返回值为size_t,是无符号的(易错)

4.学会strlen函数的模拟实现。

#include <stdio.h>
int mainO)
{
const charstr1="abcdef";
const char*str2="bbb";
if(strlen(str2)-str1en(str1)>0)
{
printf("str2>str1\n");
else
{
printf("srti>str2\n");
}
return 0;
}

strcpy

char* strcpy(char*destinationconstchar*source);

.Copiesthe Cstring pointed by source into the array pointed by destination,including the terminating nullcharacter (and stopping at that point).

1.源字符串必须以\0'结束。

2.会将源字符串中的\0'拷贝到目标空间。

3.目标空间必须足够大,以确保能存放源字符串。

4.目标空间必须可变。

5.学模拟实现。

strcat

char * strcat (char*destinationconstchar*source):

·Appends a copy of the source string to the destination string.The terminating ull character in

destination is overwritten by the first character of sourceand a null-character is included at the

end ofthe new string formed by the concatenationof both indestination.

1.源字符串必须以\0结束。

1.目标空间必须有足够的大,能容纳下源字符串的内容。

2.目标空间必须可修改。

3.字符串自己给自己追加,如何?

strcmp

int strcmp ( const char* str1 const char str2);

This function starts comparing the first character ofeach string. If they are equal to each other, it

continues with the following pairs until the characters differ or until a terminating null-character is

reached.

标准规定:

o第一个字符串大于第二个字符串,则返回大于0的数字

o第一个字符串等于第二个字符串,则返回0

o第一个字符串小于第二个字符串,则返回小于0的数字

那么如何判断两个字符串?

strncp

char* strncpy (char* destination const charsource,sizet num);

e Copies the first num characters of source to destination.If the end ofthe sourceCstring(which is

signaled by a null-character) is found before num characters have been copieddestination is

padded with zeros until a total of num characters have been written to it.

拷贝num个字符从源字符串到目标空间。

如果源字符串的长度小于num,则拷贝完源字符串之后,在目标的后边追加0,直到num个

strncat

charstrncat (char destination,const charsourcesize num);

.Appends the first num characters of source to destinationplus a terminating null-character.

e Ifthe length of the Cstring in source is less than um,only the content up to the terminating null

character is copied.

/*strncat example"/
#include <stdio.h>
dinclude <string.h>
int main
{
 char str1[20];
 char str2[20];
strcpy(str1,"To be");
strcpy(str2 "or not to be");
strncat(strl,str2,6);
puts(str1);
retorn 0;
}

strncmp

int strncmp (const char * str1, const char*str2, size_t num );

比较到出现另个字符不一样或者一个字符串结束或者num个字符全部比较完。

/strncmp examplee/
#include <stdio.h>
#incTude sstring.h>
int main
char str[][5]={"R2D2","C3PO","R2A6"};
inr n;
puts (Looking for R2 astromech droids...");
for (n=0 ;n<3;n++)
if(strncmp(str[n]"R2xx2)-0)
{
printf("found%s\n"str[n]);
}
return 0;}

strstr查找字符串

char*strstr(const char*=const char*);

Returns a pointer to the first occurrence of str2 Instr1or a nuill pointer if str2 Is not part of str1..

strtok

charstrtok(char*str,const char*sep);

sep参数是个字符串,定义了用作分隔符的字符集合。

第一个参数指定一个字符串,它包含了0个或者多个由sep字符串中一个或者多个分隔符分割的标

记。

strtok函数找到str中的下一个标记,并将其用\0结尾,返回一个指向这个标记的指针。

(注:strtok函数会改变被操作的字符串,所以在使用strtok函数切分的字符串一般都是临时拷贝的内

容并且可修改。)

strtok函数的第一个参数不为NULL,函数将找到str中第一个标记,strtok函数将保存它在字符串中

的位置。

strtok函数的第一个参数为NULL,函数将在同一个字符串中被保存的位置开始,查找下一个标记。

如果字符串中不存在更多的标记,则返回NULL指针。

strerror

chars strerror (int errnum ):

返回错误码,所对应的错误信息。

/* strerror example :error list*/

#include <stdio.h>

#include <string.h>

#include<errno.h>//必须包含的头文件

函数 如果他的参数符合下列条件就返回真
iscntrl 任何控制字符
isspace 空白字符:空格'’,换页\f,换行\回车\r’,制表符\t或者垂直制表符\v
isdigit 十进制数字0~9
isxdigit 十六进制数字,包括所有+进制数字,小写字母a~f,大写字母A~F
islower 小写字母a~z
isupper 大写字母A~Z
isalpha 字母a~z或A~Z
isalnum 字母或者数字,a~z,A~Z0~9
ispunct 标点符号,任何不属于数字或者字母的图形字符(可打印)
isgraph 任何图形字符
isprint 任何可打印字符,包括图形字符和空白字符

memmove

voidememmove(void*destination,const void*sourcesizet num );

和memcpy的差别就是memmove函数处理的源内存块和目标内存块是可以重叠的。

如果源空间和目标空间出现重叠,就得使用memmove函数处理。

memcmp

int memcmp ( const void*ptr1,const void * ptr2,size_t num );

比较从ptr1和ptr2指针开始的num个字节。返回值如下:

猜你喜欢

转载自blog.csdn.net/2301_77479336/article/details/130179412