Tuesday, 13 September 2016

Hacker rank solution for Strings

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 the 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.

Check This- Hacker rank Solution for Sub Domain Introduction, Classes, STL, Inheritance.

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();

Also check- Geeksforgeeks solution for SchoolBasicEasy, Medium, Hard Domain.

Now print the both 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;

Copy the full solution or colored code and paste into the hacker rank editor and click to Run Code if the code runs successfully then click to Submit Code. 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

Tip:- Before copy the program I recommended please read this full article, this will help you to build your own logic. If you have any query feel free to share with me, and if you like my work please share, comments and subscribe for our new and latest post related to programming this will help you update all about new post this Article.

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


Hacker rank solution for Strings

Hacker rank solution for Strings


You May Like This:-












No comments:
Write comments

Recommended Posts × +