C Strings tutorial - JohnHau/mis GitHub Wiki

https://www.tutorialspoint.com/cplusplus/cpp_strings.htm

image

image

image

Following example makes use of few of the above-mentioned functions −

Live Demo #include #include

using namespace std;

int main () {

char str1[10] = "Hello"; char str2[10] = "World"; char str3[10]; int len ;

// copy str1 into str3 strcpy( str3, str1); cout << "strcpy( str3, str1) : " << str3 << endl;

// concatenates str1 and str2 strcat( str1, str2); cout << "strcat( str1, str2): " << str1 << endl;

// total lenghth of str1 after concatenation len = strlen(str1); cout << "strlen(str1) : " << len << endl;

return 0; } When the above code is compiled and executed, it produces result something as follows −

strcpy( str3, str1) : Hello strcat( str1, str2): HelloWorld strlen(str1) : 10 The String Class in C++ The standard C++ library provides a string class type that supports all the operations mentioned above, additionally much more functionality. Let us check the following example −

Live Demo #include #include

using namespace std;

int main () {

string str1 = "Hello"; string str2 = "World"; string str3; int len ;

// copy str1 into str3 str3 = str1; cout << "str3 : " << str3 << endl;

// concatenates str1 and str2 str3 = str1 + str2; cout << "str1 + str2 : " << str3 << endl;

// total length of str3 after concatenation len = str3.size(); cout << "str3.size() : " << len << endl;

return 0; } When the above code is compiled and executed, it produces result something as follows −

str3 : Hello str1 + str2 : HelloWorld str3.size() : 10

⚠️ **GitHub.com Fallback** ⚠️