-
Notifications
You must be signed in to change notification settings - Fork 2
/
day30.sol
42 lines (36 loc) · 1.11 KB
/
day30.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.5.0 <0.9.0;
contract Day30 {
function length(string memory str) public pure returns (uint256) {
bytes memory str_bytes = bytes(str);
return str_bytes.length;
}
function concatenate(string memory str1, string memory str2)
public
pure
returns (string memory)
{
bytes memory str_bytes1 = bytes(str1);
bytes memory str_bytes2 = bytes(str2);
string memory str = new string(str_bytes1.length + str_bytes2.length);
bytes memory str_bytes = bytes(str);
uint256 k = 0;
for (uint256 i = 0; i < str_bytes1.length; i++) {
str_bytes[k] = str_bytes1[i];
k++;
}
for (uint256 i = 0; i < str_bytes2.length; i++) {
str_bytes[k] = str_bytes2[i];
k++;
}
return string(str_bytes);
}
// A simpler solution
function concatenateStrings(string memory str1, string memory str2)
public
pure
returns (string memory)
{
return string(abi.encodePacked(str1, str2));
}
}