#include<iostream>
#include<cstdlib>
#include<cctype>
#include <iomanip>
#include <cstring>
//----------------------
using std :: cin;
using std :: cout;
using std :: endl;
using std::setw ;
//-------------------
const int MAX_STR_LEN=100;
//-----------------------
int main()
{
char str1 [MAX_STR_LEN];
char str2 [MAX_STR_LEN];
int i;
cin >> setw(MAX_STR_LEN)>>str1;
cin >> setw(MAX_STR_LEN)>>str2;
if (strcmp(str1, str2)!=0)
{
for (int i=0; i < strlen(str1); i++)
{
for (int j=0; j < strlen(str2); j++)
{
if (str1[i]==str2[j]) // we want to check whats common first
// and then cout whats left (not common)
{
cout << str2[j];
}
}
}
return EXIT_SUCCESS;
}
如果我正在寻找常见的角色,这完全没问题。
我试过这个循环:if (str1[i]!=str2[j])
但由于它在for loops
中,它给了我i
和所有字符串j
之间的区别。例如,如果str1=abc
和str2=abcd
答案应为d
答案 0 :(得分:3)
如果您不能更改字符串本身,则可以使用std::set
容器的方法。例如
#include <iostream>
#include <string>
#include <set>
#include <algorithm>
#include <iterator>
int main()
{
std::string s1( "abd" );
std::string s2( "ace" );
std::set<char> set1( s1.begin(), s1.end() );
std::set<char> set2( s2.begin(), s2.end() );
std::set_symmetric_difference( set1.begin(), set1.end(), set2.begin(), set2.end(),
std::ostream_iterator<char>( std::cout ) );
std::cout << std::endl;
}
输出
bcde
至少该程序会为字符串d
和abc
提供预期结果abcd
。:)
否则,您可以对字符串进行排序并应用相同的标准算法std::set_symmetric_difference
。
答案 1 :(得分:1)
这个怎么样:
#include <iostream>
#include <string>
using namespace std;
int main()
{
std::string str1, str2;
int i;
getline(cin, str1);
getline(cin, str2);
if(str1 != str2)
{
bool restart = true;
while(restart)
{
restart = false;
const int str1Len = str1.size();
for (int i=0 ; i<str1Len ; i++)
{
const int str2Len = str2.size();
for (int j=0 ; j<str2Len ; j++)
{
if (str1[i]==str2[j])
{
str1 = str1.erase(i,1);
str2 = str2.erase(j,1);
restart = true;
break;
}
}
if(restart)
break;
}
}
}
cout << "str1: " << str1 << endl;
cout << "str2: " << str2 << endl;
return 0;
}
在C ++中,您可以使用std::string
,因此您不必使用固定字符数组。我猜你不想要那个。因为您没有将其标记为C ++ 11,所以我没有使用for-each循环。我确信它不会改变结果。
答案 2 :(得分:0)
编写现代C ++代码总是产生更小,更紧凑的代码,与使用C ++随机位编写C代码相比:
#include <iostream>
#include <string>
int main()
{
std::string str1, str2;
std::getline(std::cin, str1);
std::getline(std::cin, str2);
size_t i;
for (i=0; i<str1.size() && i<str2.size(); ++i)
if (str1[i] != str2[i])
break;
std::cout << str1.substr(i) << std::endl;
std::cout << str2.substr(i) << std::endl;
return 0;
}
不要试图弄清楚两个字符串的哪些部分是不同的,你会发现在另一端思考问题要容易得多,也更简单:找出每个字符串的哪些初始部分是相同的其他。那么,剩下的就是你的答案。
在原始版本中,将每个字符串限制为100个字符并不重要,并且不会影响算法,因此我将其删除了。如果您仍然希望这样做,那么更改也将是微不足道的,我将把这部分作为您的家庭作业。
答案 3 :(得分:0)
很奇怪,我打算编辑你的问题,但我意识到这就是答案。 你错过了第一个if语句的结束括号,我把它放进去了。 你正在用strlen()比较一个int和一个unsinged int,我将它留在100个字符限制的conext中,它很好。 int i = 0;不是你的for循环中的相同的int,并且是未使用的。我评论说出来了。 有没有理由不使用命名空间std? 那些其他头文件不是必需的。 添加了一个换行符,所以我可以很容易地看到输出结果。
我不确定exit_success是什么,我会使用return 0;
#include<iostream>
#include<cstdlib>
#include <iomanip>
//----------------------
using namespace std;
//-------------------
const int MAX_STR_LEN=100;
//-----------------------
int main()
{
char str1 [MAX_STR_LEN];
char str2 [MAX_STR_LEN];
// int i;
cin >> setw(MAX_STR_LEN)>>str1;
cin >> setw(MAX_STR_LEN)>>str2;
if (strcmp(str1, str2)!=0)
{
for (int i=0; i < strlen(str1); i++)
{
for (int j=0; j < strlen(str2); j++)
{
if (str1[i]==str2[j]) // we want to check whats common first
cout << str2[j];
}
}
cout <<"\n";
}
return 0;
}
我怀疑这是你想要的答案。当然,它的C / C ++风味又是什么?