Hacker rank solution for Strings In C++

Problem:- Hacker Rank Solution Program In C++ For " Strings " or Hacker Rank Solution Program In C++ For Strings or strings solution hacker rank or Hacker rank solution for c++ domain or Hacker rank solution for string sub domain or strings solution in c++ of hacker rank.

Logic:- First we calculate the size of both string after that we print both string then after we can swap the first character of both string and after that print the string again. below is the solution of the problem with an explanation.

Explanation:- As we discuss above first calculate the size of both string, we can either use a size() function or loop for calculating the size of the string. 

Size of String

string str1 = "Ghanendra"
string str2 = "Yadav"

int len1 = str1.size();
int len2 = str2.size();

Check- Geeksforgeeks solution for SchoolBasicEasy, Medium, Hard Domain.

Now print the string without space, if we print space between both strings the program will generate an error. first, we print first string then after second-string according to our program requirement and at the end use endl, this will move the cursor in the next line.


Now the last step is to swap the both string first character so we can find our desired solution. swapping a character of both strings done by taking an extra character, with the help of extra character we can perform the swap action. And in the last print the both strings with space.

Swapping The Character

char c;
cout<<str1<<" "<<str2;

Before pasting the code into the editor make sure you have chosen c++ editor in the top right drop down option.

Submit your solution here:- Click here


#include <iostream>
#include <string>
using namespace std;
int main()

  string a,b;


    int len1 = a.size();
    int len2 = b.size();

    cout<<len1<<" "<<len2<<endl;


    char c;

    cout<<a<<" "<<b;

    return 0;

Output :-

Hacker rank solution for Strings

Hacker rank solution for Strings

You May Like This:-

Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me