07/03/2023

Strings Hackerrank Solution in C++ | Hackerrank Solutions

Strings Hackerrank Solution in C++ or build a string hackerrank solution or hackerrank string problems or string function calculation hackerrank solution. 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: First, we calculate the size of both strings after that we print both strings then after we can swap the first character of both strings and after that print the string again. below is the solution to the problem with an explanation.

Strings Hackerrank Solution in C++

Declaration:

string a = "abc";

Size:

int len = a.size();

Concatenate two strings:

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

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').

Tips: Before pasting the code into the editor make sure you have chosen the C++ editor in the top right drop-down option.

Submit your solution here: Click here

Strings Hackerrank Solution in C++


#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;
}

As we discuss above first calculate the size of both strings, 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();

Now print the string without space, if we print space between both strings the program will generate an error. first, we print the first string then after the second string according to our program requirement and at the end use endl, this will move the cursor to the next line.

cout<<str1<<str2<<endl;

Now the last step is to swap both strings' first characters so we can find our desired solution. swapping a character of both strings is done by taking an extra character, with the help of an extra character we can perform the swap action. And in the last print both strings with space.

Swapping The Character

char c;

c=a[0];
str1[0]=str2[0];
str2[0]=c;

cout<<str1<<" "<<str2;

Strings Hackerrank Solution Output


Strings Hackerrank Solution Output


The Output of Strings Hackerrank Solution

Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

0 Comments: