pages:
  • 1
Daneshvar Amrollahi SAYS

Hi. I have some questions: 1 - Can we read it with string or we should read it by an array of chars?

2 - I'm sure about my code. It gives the exact output. Can printing a blank line at the end take wrong answer? This is my code. It's very short. I'd be thankful if anyone could check it. Plz...

#include<iostream>
using namespace std;
int main()
{
string s,a1,a2;
cin>>a1>>a2;
cout<<a2<<endl<<a1;
while (getline(cin,s))
{
    for (int i=0;i<s.length();i++)
    {
        bool find = false;
        for (int j=0;j<a1.length();j++)
        {
            if ((s[i])==a1[j])
            {
                cout<<a2[j];

                find = true;
                break;
            }
        }
        if (find==false)
            cout<<s[i];
    }
    cout<<endl;
}
return 0;
}
AMiR SAYS

It seems you have managed to solve the problem anyway!

As you may know now, you should use getline to read the first two lines too :)

Printing blank lines - as far as your printable characters are exactly the same as the correct outputs - will leads you to Presentation Error.

BTW here is my code for this problem:

#include <string>
#include <iostream>
using namespace std;
int main () {
    string r, f, t;
    getline(cin, r); getline(cin, f);
    cout << f << endl << r << endl;
    while (getline(cin, t)) {
        for (int i=0;i<t.length();i++)
            cout << (r.find_first_of(t[i]) == -1? t[i] : f[r.find_first_of(t[i])]);
        cout << endl;
    }
}
Daneshvar Amrollahi SAYS

Thanks. It's solved now.