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"
Solution:
#include <iostream>
#include <string>
using namespace std; int main() {
string str,str1;
int a,b;
cin>>str>>str1;
a=str.size();
b=str1.size();
cout<<a<<" "<<b<<endl;
cout<<str+str1<<endl;
char c=str[0];
str[0]=str1[0];
str1[0]=c;
cout<<str<<" "<<str1;
return 0;
}
|
Note:
This Code is Verified by all Test Cases.If any error occurs then Comment correct code Below in comment box.
Disclaimer:-
The above hole problem statement is given by hackerrank.com, but the solution is generated by the CodexRitik . if any of the query regarding this post or website fill the following contact form Thank You.
No comments: