<<Strings>>_HakerRank_C++_Strings

#include <iostream>#include <string>using namespace std;int main() {// Complete the program    string s1, s2 ;        cin >> s1 >> s2 ;        /*In the 1st line print two space-separated integers,             representing the length of a and b respectively.*/    cout << s1.size() << " " << s2.size() << endl;        /*In the 2nd line print the string produced     by concatenating a and b (a+b).*/    cout << s1 << s2 << endl ;        /*In the 3rd line print a' and b' that the same as a and b,    except that their first characters are swapped.*/    cout << s2.substr(0,1) << s1.substr(1) << " "         << s1.substr(0,1) << s2.substr(1) ;        return 0;}

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 iᵗʰ 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
abcd
ef

Sample Output
4 2
abcdef
ebcd af

Explanation
• a = "abcd"
• b = "ef"
• │a│ = 4
• │b│ = 2
• a + b = "abcdef"
• a' = "ebcd"
• b' ="af"


关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章