Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point).
源字符串必须以 '\0' 结束。
会将源字符串中的 '\0' 拷贝到目标空间。
目标空间必须足够大,以确保能存放源字符串。目标空间必须可变。
作用:
先将 these 复制到 str 中,在逐次将 strings 、are、concatenated 连接到str上
结果:
注意:
Appends a copy of the source string to the destination string. The terminating null character in destination is overwritten by the first character of source, and a null-character is included at the end of the new string formed by the concatenation of both in destination.
1.源字符串必须以 '\0' 结束。
2.目标空间必须有足够的大,能容纳下源字符串的内容。目标空间必须可修改。
This function starts comparing the first character of each 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.
标准规定:
第一个字符串大于第二个字符串,则返回大于0的数字
第一个字符串等于第二个字符串,则返回0
第一个字符串小于第二个字符串,则返回小于0的数字
比较是比的相应字符的ASCII码值,如果相等,则比较下一个以此类推.....
模拟实现:
int my_strcmp(const char* src, const char* dest)
{
int ret = 0;
assert(src != NULL);
assert(dest != NULL);
while (!(ret = *(unsigned char*)src - *(unsigned char*)dest) && *dest)//dest不为空,且如果相减结果为0,就继续循环
{
++src, ++dest;
if (ret < 0)
ret = -1;
else if (ret > 0)
ret = 1;
}
return(ret);
}
#include<assert.h>
int my_strcmp(const char*s1, const char*s2)
{
assert(s1 && s2);
while (*s1 == *s2)
{
if (*s1 == '\0')
return 0;
s1++;
s2++;
}
return *s1 - *s2;
}
3.长度受限制的字符串函数介绍 1.strncpy-从原始串复制自定义个字符到新串中
介绍:
实例:
#include <stdio.h>
#include <string.h>
int main ()
{
char str1[]= "To be or not to be";
char str2[40];
char str3[40];
/* copy to sized buffer (overflow safe): */
strncpy ( str2, str1, sizeof(str2) );
/* partial copy (only 5 chars): */
strncpy ( str3, str2, 5 );
str3[5] = '\0'; /* null character manually added */
puts (str1);
puts (str2);
puts (str3);
return 0;}
作用:
将str1中所有的字符复制到str2中
将str1中的前五个字符复制到str3中
结果:
注意:
Copies the first num characters of source to destination. If the end of the source C string (which is
signaled by a null-character) is found before num characters have been copied, destination is padded
with zeros until a total of num characters have been written to it.
拷贝num个字符从源字符串到目标空间。
如果源字符串的长度小于num,则拷贝完源字符串之后,在目标的后边追加0,直到num个。
#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;
}
作用:
将str2中的前6个字符连接到str1后面
结果:
注意:
Appends the first num characters of source to destination, plus a terminating null-character.
If the length of the C string in source is less than num, only the content up to the terminating nullcharacter is copied.
#include <stdio.h>
int main()
{
FILE* pFile;
pFile = fopen("unexist.ent", "rb");
if (pFile == NULL)
perror("The following error occurred");
else
fclose(pFile);
return 0;
}
结果:
6.字符分类函数:
注意:
比较时,是一个一个字符的去比较
示例及结果:
#include <stdio.h>
#include <ctype.h>
int main ()
{
int i;
char str[]="c3po...";
i=0;
while (isalnum(str[i])) i++;
printf ("The first %d characters are alphanumeric.\n",i);
return 0;
}
#include <stdio.h>
#include <ctype.h>
int main()
{
int i = 0;
char str[] = "C++";
while (str[i])
{
if (isalpha(str[i]))
printf("character %c is alphabetic\n", str[i]);
else
printf("character %c is not alphabetic\n", str[i]);
i++;
}
return 0;
}
#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;
}
#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;
}
结果:
注意:
比较从ptr1和ptr2指针开始的num个字节
返回值同strcmp
模拟实现:
int my_memcmp(const void* p1, const void* p2, size_t count)//方法1
{
assert(p1);
assert(p2);
char* dest = (char*)p1;
char* src = (char*)p2;
while (count-- && (*dest++ == *src++))
{
;
}
if (count == 0)
return 0;
return *dest - *src;
}