#include <cstring>
#include <iostream>
using namespace std;
int main()
{
char abc [ ] = "abc";
char def [ ] = "def";
char ghi [ ] = {'g', 'h', 'i'};
char jklmnop[ ] = "jklmnop";
char buffer [7] = {'A', 'A', 'A', 'A', 'A', 'A', 'A'};
cout << sizeof(abc) << " " << sizeof(def) << " " << sizeof(ghi) << " : ";
cout << strnlen_s(abc, sizeof(abc) - 1) << " " <<
strnlen_s(def, sizeof(def) - 1) << " " <<
strnlen_s(ghi, sizeof(ghi) - 1) << " ";
strcpy_s(buffer, sizeof(buffer), abc);
cout << buffer << " ";
buffer[6] = 'A';
strncpy_s(buffer, jklmnop, sizeof(buffer) - 1);
cout << buffer << " ";
buffer[6] = 'A';
strcpy_s(buffer, sizeof(buffer), abc);
strcat_s(buffer, sizeof(buffer), def);
cout << buffer << " ";
buffer[6] = 'A';
strcpy_s(buffer, sizeof(buffer), abc); // Buffer needs a '\0' in it.
strncat_s(buffer ,
sizeof(buffer),
jklmnop ,
sizeof(buffer) - strnlen_s(buffer, sizeof(buffer) - 1) - 1);
cout << buffer << " ";
cout << endl;
return 0;
}
// Output: 4 4 3 : 3 3 2 abc jklmno abcdef abcjkl
References:http://en.cppreference.com/w/c/string/byte/strlen
http://en.cppreference.com/w/c/string/byte/strcpy
http://en.cppreference.com/w/c/string/byte/strncpy
http://en.cppreference.com/w/c/string/byte/strcat
http://en.cppreference.com/w/c/string/byte/strncat
No comments:
Post a Comment