C++ 字符串 2-- 18.6~18.10 string型字符串

80 篇文章 0 订阅
#include <iostream>
#include <string>
using namespace std;
/*---------------------------------
     18-02 18.6~18.10 string型字符串
---------------------------------*/
int check(char des[],char sor[]);
int main()
{
	string str="string字符串";
	char ch[]="char字符串";
	cout<<str<<endl;
	cout<<ch<<endl;
	cout<<"请输入“狗”的英文单词:"<<endl;
	cin>>str;
	if(str=="dog")
	{cout<<"狗:"<<str<<endl;}
	else
	{cout<<"输入错误"<<endl;}
	cout<<"请输入“猪”的英文单词:"<<endl;
	cin>>ch;
	//if(ch=="pig")           //这种方式是错误,相当于是在比较两个地址
	if(0==check("pig",ch))    //自定义的比较函数
	//if(0==strcmp("pigf",ch)) //库函数
	{cout<<"猪:"<<ch<<endl;}
	else
	{cout<<"输入错误"<<endl;}


	return 0;
}


int check(char des[],char sor[])
{
	bool quit=false;
	if(strlen(des)!=strlen(sor))
		return 1;
	for(int i=0;i<strlen(des);i++)
	{
		if(des[i]!=sor[i])
		{
			quit =true;
			break;
		}
	}
	if(quit==false)
	{
		return 0;
	}
	return 1;


}

运行结果:

string字符串
char字符串
请输入“狗”的英文单词:
dog
狗:dog
请输入“猪”的英文单词:
pig
猪:pig
Press any key to continue

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值