Skip to content
Image
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

HackerRank Strings solution in C++ programming

Image YASH PAL, 31 July 202414 January 2026

HackerRank C++ Strings solution – In this HackerRank Strings problem in the C++ programming language, 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 ith 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.

HackerRank Strings solution in c++ programming

HackerRank Strings problem solution in C++.

#include <bits/stdc++.h>

using namespace std;

int main()
{
    string a, b;
    cin >> a >> b;
    
    cout << a.length() << ' ' << b.length() << endl;
    
    cout << a + b << endl;
    
    swap( a[0], b[0] );
    cout << a << ' ' <<  b << endl;
    
    return 0;
}

Second solution

#include <iostream>
#include <string>
using namespace std;

int main() {
    string a,b;
    char A,B;
    cin>>a;
    cin>>b;
    cout << a.length() << " " << b.length() << "n";
    cout << a << b << "n";
    A = a[0];
    B = b[0];
    a[0] = B;
    b[0] = A;
    cout << a << " " << b << "n";
  
    return 0;
}
C++ Solutions coding problems solutions Hackerrank Problems Solutions cppHackerRank

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

HackerRank C++ Problems Solutions
Say “Hello, World!” with C++ solution
Input and Output solution
Basic Data Types solution
Conditional Statements solution
For loop solution
Functions solution
Pointer solution
Arrays Introduction solution
Variable-Sized Arrays solution
Attribute Parser solution
StringStream solution
Strings solution
Structs solution
Class solution
classes and objects solution
Box It! solution
Inherited code solution
Exceptional server solution
Virtual Functions solution
Abstract Classes — Polymorphism solution
Vector-Sort solution
Vector-Erase solution
Lower Bound-STL solution
Sets-STL solution
Maps-STL solution
Print Pretty solution
Deque-STL solution
Inheritance Introduction solution
Hotel Prices solution
Cpp exception handling solution
Rectangle Area solution
Multi-Level Inheritance solution
Overloading Ostream Operator solution
Messages Order solution
Accessing Inherited Functions solution
Magic Spells solution
C++ Class Templates problem solution
Preprocessor Solution
Operator Overloading solution
Overload Operators solution
Attending workshops solution
C++ Class Template Specialization solution
C++ Variadics problem solution
Bit Array solution

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes
Advertisement