Tuesday, 13 September 2016

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.

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.

Submit your solution here:- Click here

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












Extreme Recommended:- Like our Facebook Page or Join our Facebook Group and Google plus Community for up-to-date. If you have any Query or Question you can ask in the group, I will Try To Solve your Query and try to answers of your Questions withing 24 Hours, You can also Email me or comment below Please suggest to your Friends to join and like our page and don't forget to Subscribe. Enter your Email and click to subscribe.

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