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.
Declaration:
string a = "abc";
Size:
Size:
int len = a.size();
Concatenate two strings:
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
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;
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;
0 Comments: