Strings in C++ – Hacker Rank Solution | HackerRank Programming Solutions | HackerRank C++ Solutions

Hello Programmers/Coders, Today we are going to share solutions of Programming problems of HackerRank of Programming Language C++ . At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight you profile to the recruiters.

In this post, you will find the solution for Strings in C++-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.

Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.

Introduction To C++

C++ is a general-purpose programming language that was developed as an enhancement of the C language to include object-oriented paradigm. It is an imperative and a compiled language. 

C++ is a middle-level language rendering it the advantage of programming low-level (drivers, kernels) and even higher-level applications (games, GUI, desktop apps etc.). The basic syntax and code structure of both C and C++ are the same. 

C++ programming language was developed in 1980 by Bjarne Stroustrup at bell laboratories of AT&T (American Telephone & Telegraph), located in U.S.A. Bjarne Stroustrup is known as the founder of C++ language.

Strings in C++ 


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:

string a = "abc";


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 :


Sample Output :

4 2
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 
    //string two
    //first string size
    strlen1 = str1.size();
    //second string size
    strlen2 = str2.size();
    cout<<" "<<strlen2;
    // concatinate two string
    str3 = str1+str2;
    // a' b'
    a = str2[0];
    b = str1[0];        
    str1[0] = a;
    str2[0] = b;
    cout<<" "<<str2;
    return 0;

Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock