Skip to content

Commit

Permalink
[cpp/en] String length content addition (#4888)
Browse files Browse the repository at this point in the history
* Add string.length() and string.size() example line

Adds a line under the Strings section to add total string length by
calculating the sum of two strings which have been given on output above
with their sizes found out using string::length() and string::size()
functions, with appropriate comment on this line.

Signed-off-by: Harshit Gupta <[email protected]>

* Add cstring strlen() example

Includes <cstring> header to demonstrate length of a character array
string which is C-compliant by using strlen() function available from
this header supported in C++.
Declared string is initialized with char array size [10] to just fit the
string contents with '\0' terminator.

Signed-off-by: Harshit Gupta <[email protected]>

---------

Signed-off-by: Harshit Gupta <[email protected]>
  • Loading branch information
Git-Harshit authored Apr 7, 2024
1 parent 0aceb41 commit 2891ecf
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions c++.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,19 @@ cout << myString + myOtherString; // "Hello World"

cout << myString + " You"; // "Hello You"

// C++ string length can be found from either string::length() or string::size()
cout << myString.length() + myOtherString.size(); // Outputs 11 (= 5 + 6).

// C++ strings are mutable.
myString.append(" Dog");
cout << myString; // "Hello Dog"

// C++ can handle C-style strings with related functions using cstrings
#include <cstring>

char myOldString[10] = "Hello CPP";
cout << myOldString;
cout << "Length = " << strlen(myOldString); // Length = 9

/////////////
// References
Expand Down

0 comments on commit 2891ecf

Please sign in to comment.