1、strcat
char * strcat ( char * destination, const char * source );
连接字符串
将source字符串的拷贝附加到destination字符串,destination中的结束空字符null被source字符串的第一个字符所覆盖,并且一个新的空字符null被附加到连接两个字符串而生成的新字符串destination的末尾。
参数
destination
指向destination数组的指针,它包含一个C字符串,并且应该足够大以便可以容纳连接后的结果字符串。
source
被附加到destination的C字符串。它不应该与destination重叠。
返回值
返回destination。
Example:
/* strcat example */
#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;
}
Output:
these strings are concatenated.
2、strchr
const char * strchr ( const char * str, int character );
char * strchr ( char * str, int character );
定位一个字符在字符串中的第一次出现返回一个指向在C字符串str中字符character第一次出现的位置的指针。
结束空字符null被算作是C字符串的一部分。因此,它也可以被定位,以便获取一个指向字符串末尾的指针。
参数
str
C字符串。
character
需要被定位的字符。它被作为整型值传递,但是他最终是被转换回char类型。
返回值
返回一个指向在str中character第一次出现的位置的指针。
拂过没有找到,则返回空指针null。
可移植性
在C语言中,这个函数被声明为:
char * strchr ( const char *, int );
而不是C++中提供的两个冲在版本。Example:
/* strchr example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] = "This is a sample string";
char * pch;
printf ("Looking for the 's' character in \"%s\"...\n",str);
pch=strchr(str,'s');
while (pch!=NULL)
{
printf ("found at %d\n",pch-str+1);
pch=strchr(pch+1,'s');
}
return 0;
}
Output:
Looking for the 's' character in "This is a sample string"...
found at 4
found at 7
found at 11
found at 18
3、strcmp
int strcmp ( const char * str1, const char * str2 );
比较两个字符串比较C字符串str1和C字符串str2.
这个函数从比较两个字符串的第一个字符开始。如果这两个字符相等,则比较余下的字符对直到两个字符不相等或到达结束空字符null。
参数
str1
要比较的C字符串。
str2
要比较的C字符串。
返回值
返回一个指示两个字符串之间的关系的整型值:
0值指示两个字符串是相等的。
大于0的值指示,对第一个不匹配的字符,str1的字符值大于str2的,反之则返回一个小于0的值。
Example:
/* strcmp example */
#include <stdio.h>
#include <string.h>
int main ()
{
char szKey[] = "apple";
char szInput[80];
do {
printf ("Guess my favourite fruit? ");
gets (szInput);
} while (strcmp (szKey,szInput) != 0);
puts ("Correct answer!");
return 0;
}
Output:
Guess my favourite fruit? orange
Guess my favourite fruit? apple
Correct answer!
4、strcoll
int strcoll ( const char * str1, const char * str2 );
功能和strcmp类似,用法也一样。
特别注意:
strcoll()会依环境变量LC_COLLATE所指定的文字排列次序来比较s1和s2 字符串。
strcmp是根据ASCII来比较2个串的。
说明:若LC_COLLATE为"POSIX"或"C",则strcoll()与strcmp()作用完全相同。
5、strcpy
char * strcpy ( char * destination, const char * source );拷贝字符串
将source指向的C字符串拷贝到destination指向的数组,包括结束空字符串null。
为了避免溢出,destination指向的数组的大小应该足够长以便能容纳C字符串source(包括结束空字符串null),并且在内存中不能与source重叠。
参数
destination
指向要复制内容的目的数组的指针。
source
要复制的C字符串。
返回值
返回destination。
Example:
/* strcpy example */
#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;
}
Output:
str1: Sample string
str2: Sample string
str3: copy successful
6、strcspn
to be continued...