c++字符串处理函数


Posted on June 27, 2007


strcpy:将字符串source拷贝到字符串destination中。
\r\nstrcpy函数应用举例
\r\n原型:strcpy(char destination[], const char source[]);
\r\n功能:将字符串source拷贝到字符串destination中
\r\n例程:

\r\n

 #include <iostream.h>
\r\n#include <string.h>
\r\nvoid main(void)
\r\n{
\r\n  char str1[10] = { "TsinghuaOK"};
\r\n  char str2[10] = { "Computer"};
\r\n  cout <<strcpy(str1,str2)<<endl;
\r\n}

\r\n

运行结果是:Computer

\r\n

第二个字符串将覆盖掉第一个字符串的所有内容!

\r\n

注意:在定义数组时,字符数组1的字符串长度必须大于或等于字符串2的字符串长度。不能用赋值语句将一个字符串常量或字符数组直接赋给一个字符数组。所有字符串处理函数都包含在头文件string.h中。

\r\n


\r\nstrncpy(char destination[], const char source[], int numchars);

\r\n

strncpy:将字符串source中前numchars个字符拷贝到字符串destination中。
\r\nstrncpy函数应用举例
\r\n原型:strncpy(char destination[], const char source[], int numchars);
\r\n功能:将字符串source中前numchars个字符拷贝到字符串destination中
\r\n例程:

\r\n

#include <iostream.h>
\r\n#include <string.h>
\r\nvoid main(void)
\r\n{
\r\n  char str1[10] = { "Tsinghua "};
\r\n  char str2[10] = { "Computer"};
\r\n  cout <<strncpy(str1,str2,3)<<endl;
\r\n}

\r\n

运行结果:Comnghua

\r\n

注意:字符串source中前numchars个字符将覆盖掉字符串destination中前numchars个字符!

\r\n

strcat(char target[], const char source[]);

\r\n

strcat:将字符串source接到字符串target的后面。
\r\nstrcat函数应用举例
\r\n原型:strcat(char target[], const char source[]);
\r\n功能:将字符串source接到字符串target的后面
\r\n例程:

\r\n

#include <iostream.h>
\r\n#include <string.h>
\r\nvoid main(void)
\r\n{
\r\n  char str1[] = { "Tsinghua "};
\r\n  char str2[] = { "Computer"};
\r\n  cout <<strcpy(str1,str2)<<endl;
\r\n}

\r\n

运行结果:Tsinghua Computer

\r\n

注意:在定义字符数组1的长度时应该考虑字符数组2的长度,因为连接后新字符串的长度为两个字符串长度之和。进行字符串连接后,字符串1的结尾符将自动被去掉,在结尾串末尾保留新字符串后面一个结尾符。
\r\n
\r\nstrncat(char target[], const char source[], int numchars);

\r\n

strncat:将字符串source的前numchars个字符接到字符串target的后面。
\r\nstrncat函数应用举例:
\r\n原型:strncat(char target[], const char source[], int numchars);
\r\n功能:将字符串source的前numchars个字符接到字符串target的后面
\r\n例程:

\r\n

#include <iostream.h>
\r\n#include <string.h>
\r\nvoid main(void)
\r\n{
\r\n  char str1[] = { "Tsinghua "};
\r\n  char str2[] = { "Computer"};
\r\n  cout <<strncat(str1,str2,3)<<endl;
\r\n}

\r\n

运行结果:Tsinghua Com

\r\n


\r\nint strcmp(const char firststring[], const char secondstring);

\r\n

strcmp:比较两个字符串firststring和secondstring。
\r\nstrcmp函数应用举例
\r\n原型:int strcmp(const char firststring[], const char secondstring);
\r\n功能:比较两个字符串firststring和secondstring
\r\n例程:

\r\n

#include <iostream.h>
\r\n#include <string.h>
\r\nvoid main(void)
\r\n{
\r\n  char buf1[] = "aaa";
\r\n  char buf2[] = "bbb";
\r\n  char buf3[] = "ccc";
\r\n  int ptr;
\r\n  ptr = strcmp(buf2,buf1);
\r\n  if(ptr > 0)
\r\n   cout <<"Buffer 2 is greater than buffer 1"<<endl;
\r\n  else
\r\n   cout <<"Buffer 2 is less than buffer 1"<<endl;
\r\n  ptr = strcmp(buf2,buf3);
\r\n  if(ptr > 0)
\r\n   cout <<"Buffer 2 is greater than buffer 3"<<endl;
\r\n  else
\r\n   cout <<"Buffer 2 is less than buffer 3"<<endl;
\r\n}

\r\n

运行结果是:Buffer 2 is less than buffer 1 
\r\n                  Buffer 2 is greater than buffer 3

\r\n

strlen( const char string[] );

\r\n

strlen:统计字符串string中字符的个数。  
\r\nstrlen函数应用举例
\r\n原型:strlen( const char string[] );
\r\n功能:统计字符串string中字符的个数
\r\n例程:

\r\n

#include <iostream.h>
\r\n#include <string.h>
\r\nvoid main(void)
\r\n{
\r\n  char str[100];
\r\n  cout <<"请输入一个字符串:";
\r\n  cin >>str;
\r\n  cout <<"The length of the string is :"<<strlen(str)<<"个"<<endl;
\r\n}

\r\n

运行结果The length of the string is x (x为你输入的字符总数字)

\r\n

注意:strlen函数的功能是计算字符串的实际长度,不包括'\0'在内。另外,strlen函数也可以直接测试字符串常量的长度,如:strlen("Welcome")。


标签:N/A

 

在线学习答案查询入口
微信扫码联系
微信扫码联系