重点介绍处理字符和字符串的库函数的使用和注意事项1、求字符串长度strlen2.长度不受限制的字符串函数strcpy strcat strcmp3、长度受限制的字符串函数介绍strncpy strncat strncmp4、字符串查找strstr strtok5、错误信息报告strerror6、内存操作函数memcpy memmove memset memcmp
1. strlen的认识和使用
size_t strlen ( const char * str );
1、字符串已经 '\0' 作为结束标志,strlen函数返回的是在字符串中 '\0' 前面出现的字符个数(不包含 '\0' )。2、参数指向的字符串必须要以 '\0'结束。3、注意函数的返回值为size_t,是无符号的( 易错 )
例子:
#include <stdio.h>
int main()
{
const char* str1 = "abcdef";
const char* str2 = "bbb";
if (strlen(str2) - strlen(str1) > 0)
{
printf("str2>str1\n");
}
else
{
printf("srt1>str2\n");
}
return 0;
}
很明显可得这个代码中打印的是 str1>str2。
1. strcpy的认识和使用
char* strcpy(char* destination, const char* source );
1、源字符串必须以'\0'结束。2、会将源字符串中的'\0'拷贝到目标空间。3、目标空间必须足够大,以确保能存放源字符串。4、目标空间必须可变。
#include <stdio.h>
#include <string.h>
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
1.3 strcat的认识和使用
char* strcat ( char* destination, const char* source );
1、源字符串必须以'\0'结束。2、目标空间必须有足够的大,能容纳下源字符串的内容。3、目标空间必须可修改。
#include <stdio.h>
#include <string.h>
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.
1.4 strcmp的认识和使用
int strcmp ( const char* str1, const char* str2 );
1、第一个字符串大于第二个字符串,则返回大于0的数字2、第一个字符串等于第二个字符串,则返回03、第一个字符串小于第二个字符串,则返回小于0的数字
#include <stdio.h>
#include <string.h>
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!
1.5 strncpy的认识和使用
char* strncpy ( char* destination, constchar* source, size_t num );
1、拷贝num个字符从源字符串到目标空间。2、如果源字符串的长度小于num,则拷贝完源字符串之后,在目标的后边追加0,直到num个。
#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = "To be or not to be";
char str2[40];
char str3[40];
strncpy(str2, str1, sizeof(str2));
strncpy(str3, str2, 5);
str3[5] = '\0';
puts(str1);
puts(str2);
puts(str3);
return 0;
}
运行结果为:
To be or not to be To be or not to be To be
1.6 strncat的认识和使用
char* strncat ( char *destination, const char* source, size_t num );
1、将源的第一个数字字符追加到目标,外加一个终止空字符。
2、如果源中 C 字符串的长度小于 ,则仅复制终止空字符之前的内容。
#include <stdio.h>
#include <string.h>
int main()
{
char str1[20];
char str2[20];
strcpy(str1, "To be ");
strcpy(str2, "or not to be");
strncat(str1, str2, 6);
puts(str1);
return 0;
}
运行结果为:To be or not
1.7 strncmp的认识和使用
intstrncmp ( const char* str1, const char* str2, size_t num );
1.比较到出现另个字符不一样或者一个字符串结束或者num个字符全部比较完
#include <stdio.h>
#include <string.h>
int main()
{
char str[][5] = { "R2D2" , "C3PO" , "R2A6" };
int n;
puts("Looking for R2 astromech droids...");
for (n = 0; n < 3; n++)
if (strncmp(str[n], "R2xx", 2) == 0)
{
printf("found %s\n", str[n]);
}
return 0;
}
运行结果为:
Looking for R2 astromech droids... found R2D2 found R2A6
1.8 strstr的认识和使用
char* strstr ( const char* str1, const char* str2);
1、返回指向 str1 中第一次出现的 str2 的指针,如果 str2 不是 str1 的一部分,则返回一个空指针。
#include <stdio.h>
#include <string.h>
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
1.9 strtok的认识和使用
char* strtok ( char* str, const char* sep );
1、sep参数是个字符串,定义了用作分隔符的字符集合2、第一个参数指定一个字符串,它包含了0个或者多个由sep字符串中一个或者多个分隔符分割的标记。、3、strtok函数找到str中的下一个标记,并将其用\0结尾,返回一个指向这个标记的指针。(注: strtok函数会改变被操作的字符串,所以在使用strtok函数切分的字符串一般都是临时拷贝的内容并且可修改。)4、strtok函数的第一个参数不为NULL,函数将找到str中第一个标记,strtok函数将保存它在字符串中的位置。5、strtok函数的第一个参数为NULL,函数将在同一个字符串中被保存的位置开始,查找下一个标 记。6、如果字符串中不存在更多的标记,则返回NULL指针。
#include <stdio.h>
#include <string.h>
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;
}
运行结果为:
Splitting string "- This, a sample string." into tokens: This a sample string
1.10 strerror的认识和使用
char* strerror ( int errnum );
返回错误码,所对应的错误信息。
#include <stdio.h>
#include <string.h>
#include <errno.h>
int main()
{
FILE* pFile;
pFile = fopen("unexist.ent", "r");
if (pFile == NULL)
printf("Error opening file unexist.ent: %s\n", strerror(errno));
return 0;
}
运行结果为:Error opening file unexist.ent: No such file or directory
1.11 memcpy的认识和使用
void* memcpy ( void* destination, constvoid* source, size_t num );
1、函数memcpy从source的位置开始向后复制num个字节的数据到destination的内存位置。2、这个函数在遇到'\0'的时候并不会停下来。3、如果source和destination有任何的重叠,复制的结果都是未定义的。
#include <stdio.h>
#include <string.h>
struct {
char name[40];
int age;
} person, person_copy;
int main()
{
char myname[] = "Pierre de Fermat";
memcpy(person.name, myname, strlen(myname) + 1);
person.age = 46;
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
1.12 memmove的认识和使用
void* memmove ( void* destination, const void* source, size_t num );
1、和memcpy的差别就是memmove函数处理的源内存块和目标内存块是可以重叠的。2、如果源空间和目标空间出现重叠,就得使用memmove函数处理。
#include <stdio.h>
#include <string.h>
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
1.13 memcmp的认识和使用
intm emcmp ( const void* ptr1, const void* ptr2, size_t num );
比较从ptr1和ptr2指针开始的num个字节返回值如下:
#include <stdio.h>
#include <string.h>
int main()
{
char buffer1[] = "DWgaOtP12df0";
char buffer2[] = "DWGAOTP12DF0";
int n;
n = memcmp(buffer1, buffer2, sizeof(buffer1));
if (n > 0) printf("'%s' is greater than '%s'.\n", buffer1, buffer2);
else if (n < 0) printf("'%s' is less than '%s'.\n", buffer1, buffer2);
else printf("'%s' is the same as '%s'.\n", buffer1, buffer2);
return 0;
}
运行结果为:'DWgaOtP12df0' is greater than 'DWGAOTP12DF0'.