C++:strcat、strcpy、strcmp、strupr、strlwr

这一部分是转载内容,应该是在旧版vs可以跑通。

#include <iostream>
#include <string>
using namespace std;
/*---------------------------------
字符串的常见处理函数
---------------------------------*/
void main()
{
	char a[20]="My name is ";  //a的空间大小定义为20,务必容纳下连接后的所有字符
	char b[]  ="jack";
	char c[]  ="jack";
	char d[]  ="ABCDEF";
	cout<<strcat(a,b)<<endl;   //返回a的指针 把字符串b连接到字符串a
	cout<<a<<endl;
	cout<<strcpy(a,b)<<endl;   //返回a的指针 把字符串b拷贝到字符串a
	cout<<a<<endl;
	if(0==strcmp(c,b))         //字符串大小比较
		cout<<"它们是相等的"<<endl;
	strupr(a);                 //小写字符转大写字符
	cout<<a<<endl;
	strlwr(d);                 //大写字符转小写字符
	cout<<d<<endl;
	cout<<"length of d[] is: "<<strlen(d)<<endl;
}
 
 
运行结果:
 
My name is jack
My name is jack
jack
jack
它们是相等的
JACK
abcdef
length of d[] is: 6

笔者用的vs2017,上面的代码会报兼容性错误,修改如下:

#include <iostream>
#include <string>
using namespace std;
#define _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS
/*---------------------------------
字符串的常见处理函数
---------------------------------*/
void main()
{
	char a[20] = "My name is ";  //a的空间大小定义为20,务必容纳下连接后的所有字符
	char b[] = "jack";
	char c[] = "jack";
	char d[] = "ABCDEF";
	cout << strcat_s(a, b) << endl;   //返回a的指针 把字符串b连接到字符串a
	cout << a << endl;
	cout << strcpy_s(a, b) << endl;   //返回a的指针 把字符串b拷贝到字符串a
	cout << a << endl;
	if (0 == strcmp(c, b))         //字符串大小比较
		cout << "它们是相等的" << endl;
	_strupr_s(a);                 //小写字符转大写字符
	cout << a << endl;
	_strlwr_s(d);                 //大写字符转小写字符
	cout << d << endl;
	cout << "length of d[] is: " << strlen(d) << endl;

		system("pause");
		return ;
}


运行结果:
0
My name is jack
0
jack
它们是相等的
JACK
abcdef
length of d[] is: 6

参考:
文章1
侵删

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值