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:-

Ghanendra Yadav

Ghanendra Yadav

Hello, I Am Ghanendra Yadav Owner of This Blog, I am professional Blogger and Programmer. I Love Programming, Logo Making, And Banner Designing. My Highest Qualification is MCA From NIT Warangal. You Can Find Me On Social Media Through Below Link And If You Have Any Query Related To Programming And Other Subject Comment Below or You Can Mail Me I Will Try To Answer Within 24 Hours Email:- yghanendra@student.nitw.ac.in

Find me on Social Media

Facebook | Twitter | Google+ | RSS Feed

No comments:

Post a Comment