Strings in C++ Hacker Rank Solution

Hello coders, In this post, you will learn how to solve the Strings in C++ Hacker Rank Solution. This problem is a part of the HackerRank C++ Programming Series.

We also provide Hackerrank solutions in CJava programming, and Python Programming languages so whatever your domain we will give you an answer in your field.

You can practice and submit all HackerRank problem solutions in one place. Find a solution for other domains and Sub-domain. I.e. Hacker Rank solution for HackerRank C ProgrammingHackerRank C++ ProgrammingHackerRank Java Programming, HackerRank Python ProgrammingHackerRank Linux ShellHackerRank SQL Programming, and HackerRank 10 days of Javascript.

Strings in C++ - Hacker Rank Solution
Strings in C++ – Hacker Rank Solution

As you already know that this site does not contain only the Hacker Rank solutions here, you can also find the solution for other problems. I.e. Web Technology,Data StructuresRDBMS ProgramsJava Programs Solutions,  Fiverr Skills Test answersGoogle Course AnswersLinkedin Assessment, and Coursera Quiz Answers.

Strings in C++ Hacker Rank Solution

Problem

C++ provides a nice alternative data type to manipulate strings, and the data type is conveniently called string. Some of its widely used features are the following:
Declaration:

string a = "abc";

Size:

int len = a.size();

Concatenate two strings:

string a = "abc";
string b = "def";
string c = a + b; // c = "abcdef".

Accessing i th element:

string s = "abc";
char   c0 = s[0];   // c0 = 'a'
char   c1 = s[1];   // c1 = 'b'
char   c2 = s[2];   // c2 = 'c'
s[0] = 'z';         // s = "zbc"

P.S.: We will use cin/cout to read/write a string.


Input Format

You are given two strings, a and b, separated by a new line. Each string will consist of lower case Latin characters (‘a’-‘z’).

Output Format

In the first line print two space-separated integers, representing the length of a and b, respectively.In the second line print the string produced by concatenating a and b(a+b).In the third line print two strings separated by a space, a’ and b’.a’ and b’ are the same as a and b, respectively, except that their first characters are swapped.


Sample Input :

abcd
ef

Sample Output :

4 2
abcdef
ebcd af

Explanation :

  •  = “abcd”
  •  = “ef”
  • |a| = 4
  • |b| = 2
  • a+b = “abcdef”
  • a’ = “ebcd”
  • b’ = “af”


Strings in C++ Hacker Rank Solution

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string str1,str2,str3;
    char b,a;
    int strlen1,strlen2;
    //string one
    cin>>str1;
    //string two
      cin>>str2;
    //first string size
    strlen1 = str1.size();
    //second string size
    strlen2 = str2.size();
    cout<<strlen1;
    cout<<" "<<strlen2;
    // concatinate two string
    str3 = str1+str2;
    cout<<"\n"<<str3;
    // a' b'
    a = str2[0];
    b = str1[0];
    str1[0] = a;
    str2[0] = b;
    cout<<"\n";
    cout<<str1;
    cout<<" "<<str2;
    return 0;
}

Disclaimer: The above Problem (Strings in C++ – Hacker Rank Solution ) is generated by Hackerrank but the Solution is Provided by Chase2Learn. This tutorial is only for Educational and Learning purposes. Authority if any of the queries regarding this post or website fill the following contact form thank you.

FAQ:

Which language is best for HackerRank?

While JavaScript is the best-known language, HackerRank also found that only 5% of respondents say it is their first programming language.

Is HackerRank good for beginners?

HackerRank is very good for beginners so even if you want to print your first program “Hello World!” then definitely HackerRank gives this opportunity to you. It has a pretty good UI with boilerplate code pre-written that helps beginners to start competitive coding.

What is meant by HackerRank?

HackerRank is a tech company that focuses on competitive programming challenges for both consumers and businesses. Developers compete by writing programs according to provided specifications. Wikipedia

What is C++ programming used for?

C++ is a general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or “C with Classes”. Wikipedia

Where can I find HackerRank solutions in CPP?

in this post you will get all the solutions of HackerRank CPP Problems.

Finally, we are now, in the end, I just want to conclude some important message for you

Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.

Please share our posts on social media platforms and also suggest to your friends to Join Our Groups. Don’t forget to subscribe.

Sharing Is Caring

Leave a Comment