C++字符串处理函数

字符连接函数的语法格式

strcat(字符数组1,字符数组2);

该函数用语连接两个字符串.字符串2连接在字符串1后,连接之后的字符串放在字符数组1中,函数返回值为字符串1的首地址.

注意:数组1必须足够大strcat(char target[], const char source[]);

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

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

运行结果:Tsinghua Computer

注意:在定义字符数组1的长度时应该考虑字符数组2的长度,因为连接后新字符串的长度为两个字符串长度之和。进行字符串连接后,字符串1的结尾符将自动被去掉,在结尾串末尾保留新字符串后面一个结尾符。

strncat(char target[], const char source[], int numchars);

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

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

运行结果:Tsinghua Com字符串拷贝函数的语法格式

******************************************************************************************************************

strcpy(char destination[], const char source[]);//(字符数组1,字符数组2)

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

注意:1.字符数组1要足够大,能放的下2

2.字符数组2可为一字符串常量,如strcpy(s1,"abc");

3./0一起拷贝

4.也可以将字符串2中若干字符拷贝到字符串1中去.如:strcpy(str1,str2,2),将str2中前2个字符拷贝到str1中再加上"/0".

例:

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

运行结果是:Computer

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

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


strncpy(char destination[], const char source[], int numchars);

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

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

运行结果:Comnghua

********************************************************************************************************************

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

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

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

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

*******************************************************************************************************************

strlen( const char string[] );

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

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

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

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

 *******************************************************************************************************************

strlwr(字符串):将字符串中大写字母转换成小写字母

strlwr(字符串):将字符串中小写字母转换成大写字母

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值