# 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"

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

Check- Geeksforgeeks solution for SchoolEasy, 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.

cout<<str1<<str2<<endl;

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;

c=a[0];
str1[0]=str2[0];
str2[0]=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.

Solution:-

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

string a,b;

cin>>a;
cin>>b;

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

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

cout<<a<<b<<endl;

char c;
c=a[0];
a[0]=b[0];
b[0]=c;

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

return 0;
}

Output :-

You May Like This:-