#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个字符!
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
int strcmp(const char firststring[], const char secondstring);
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")。
分享到:
相关推荐
自己封装的纯C++的字符串处理函数大全,像特别好用的 字符串切分 Split函数代码均已经过测试,并有接口说明,可方便调用。
C语言,C++字符串处理函数,涵盖了不少的字符串处理的算法。
C++常用字符串处理函数及使用示例
c++字符串操作函数集合整理c++字符串操作函数集合整理
一些C++ 专用的字符串处理函数集,主要包括以下函数: 包括: IsNumeric——检查串是否为数字串 Upcase——变成大写串 Lowercase——变成小写串 Trim——截掉串尾的空格 RTrim——截掉串开始处的...
c++字符串操作函数集合整理c++字符串操作函数集合整理.docx
c++字符串操作函数集合整理c++字符串操作函数集合整理借鉴.pdf
C++字符串分割源码,破解版VC助手,兼容多个windows版本
C++字符串函数大全.txtC++字符串函数大全.txt
c++字符串操作函数集合整理c++字符串操作函数集合整理[借鉴].pdf
C++字符串处理函数 字符串连接函数 strcat 其函数原型为 strcat(char[],const char[]); strcat是string catenate(字符串连接)的缩写。该函数有两个字符数组的参数,函数的作用是:将第二个字符数组中的字符串...
字符串处理函数及示例 如: 函数名: strcpy 功 能: 拷贝一个字符串到另一个 用 法: char *strcpy(char *destin, char *source); 程序例: C/C++ code #include #include int main(void) { char string[10]; char ...
字符串处理函数大全,主要包括的是c和c++
总结了C++中常见的字符串操作函数,是大家在编程中会常遇到的
本文档列出了标准C语言ANSI字符与C++宽字符的处理函数对照表,对于处理宽字符是很好的参考文件。
C语言字符串函数大全C/C++基础 函数名: stpcpy 功 能: 拷贝一个字符串到另一个 用 法: char *stpcpy(char *destin, char *source); 程序例: #include <stdio.h> #include <string.h> int main(void) ...
一份示例代码,向你展示了如何通过字符串,调用同名函数
MFC(Microsoft Foundation Classes),是一个微软公司提供的类库(class libraries),以C++类的形式封装了Windows的API,并且包含一个应用程序框架,以减少应用程序开发人员的工作量。其中包含的类包含大量Windows...
完全版C和C++字符串处理函数,很全的啊!!
本文档包含对C++字符串函数相关内容的资料以及介绍