Strings in C++

I received an automated message from HackerRank with a challenge based on strings. I incorrectly assumed it was the next one in the Introduction section of C++. After solving it I found I made an incorrect assumption. Such is life.

If interested, the challenge description may be found at:

Following is the contents of a Windows console created by Microsoft Visual Studio Professional 2013 IDE using the sample data for the challenge:



4 2


ebcd af

The output matched the test. Submitted my solution and passed all six test cases.

My solution in C++ follows:

#include <iostream>

#include <string>

using namespace std;



int main () {

string a,


// **** ****

cin >> a;

cin >> b;

cout << a.size() << ” ” << b.size() << endl;

cout << a + b << endl;

char temp = a[0];

a[0] = b[0];

b[0] = temp;

cout << a << ” ” << b << endl;

// **** ****

return 0;


If you have a comment or questions regarding this or any other post in this blog, please send me a message via email. I will not use your name unless you explicitly allow me to do so.


Follow me on Twitter: @john_canessa

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.