1、strcpy(char destination[], const char source[]);
strcpy:将字符串 source 拷贝到字符串 destination 中。
strcpy 函数应用举例
原型:strcpy(char destination[], const char source[]); 功能:将字符串 source 拷贝到字符串 destination 中
例程:
#include
#include
void main(void)
{
char str1[10] = { "
2、TsinghuaOK"};
char str2[10] = { "Computer"}; cout <3、char source[], int numchars);
strncpy:将字符串 source 中前 numchars 个字符拷贝到字符串 destination 中。
strncpy 函数应用举例
原型:strncpy(char destination[], const char source[], int numchars);
功能:将字符串 source 中前 numchars 个字符拷贝到字符串 destination 中
例程:
#include
#include
void main(void
4、)
{
char str1[10] = { "Tsinghua "}; char str2[10] = { "Computer"};
cout <5、原型:strcat(char target[], const char source[]);
功能:将字符串 source 接到字符串 target 的后面
例程:
#include
#include
void main(void)
{
char str1[] = { "Tsinghua "}; char str2[] = { "Computer"};
cout <6、符数组 1 的长度时应该考虑字符数组 2 的长度,因为连接后新字符串的长度 为两个字符串长度之和。进行字符串连接后,字符串 1 的结尾符将自动被去掉,在结尾串末
尾保留新字符串后面一个结尾符。
strncat(char target[], const char source[], int numchars);
strncat:将字符串 source 的前 numchars 个字符接到字符串 target 的后面。
strncat 函数应用举例:
原型:strncat(char target[], const char source[], int numc
7、hars);
功能:将字符串 source 的前 numchars 个字符接到字符串 target 的后面
例程:
#include
#include
void main(void)
{
char str1[] = { "Tsinghua "}; char str2[] = { "Computer"};
cout <8、tring[], const char secondstring);
strcmp:比较两个字符串 firststring 和 secondstring。
strcmp 函数应用举例
原型:int strcmp(const char firststring[], const char secondstring);
功能:比较两个字符串 firststring 和 secondstring
例程:
#include
#include
void main(void)
{
char buf1[] = "aaa"
9、 char buf2[] = "bbb"; char buf3[] = "ccc";
int ptr;
ptr = strcmp(buf2,buf1);
if(ptr > 0)
cout <<"Buffer 2 is greater than buffer 1"< 0)
cout <<"Buffer 2 is greater than buffer 3"<10、lse
cout <<"Buffer 2 is less than buffer 3"<
#include
void main(void)
{
char str[100];
cout <<"请输入一个字符串:";
cin >>str;
cout <<"The length of the string is :"<