/**
*返回共同字符
*积累.empty()
*/
#include <iostream>
#include <string>
using namespace std;
string commonChars(const string &s1,const string & s2);
int main()
{
string s1;
cout << "Enter a string s1: ";
cin >> s1;
string s2;
cout << "Enter a string s2: ";
cin >> s2;
cout << commonChars(s1,s2) << endl;
return 0;
}
string commonChars(const string &s1,const string & s2)
{
int a=s1.size();
int b=s2.size();
string common="";
for(int i=0; i<a; i++)
{
for(int j=0; j<b; j++)
{
if(s1.at(i)==s2.at(j))
{
common+=s1.at(i);
break;
}
}
}
if (common.empty())//.empty() 用于检查一个字符串是否为空
{
return "No common characters";
}
else
{
return "The common characters are "+common ;
}
}
返回两个字符串中的共同字符
最新推荐文章于 2024-10-16 22:34:48 发布