445
In this code example we will show how to get the length of a string in C++
There are 2 ways to do this in this code example
You can get the length of a string object by using a size() function or a length() function.
Example
#include <iostream> #include <cstring> using namespace std; int main() { char myString1[] = "This is my test string"; string myString2 = "This is my test string"; //using strlen cout << "strlen - String Length = " << strlen(myString1) << "\n"; //using str.size() cout << "str.size- String Length = " << myString2.size(); return 0; }
strlen - String Length = 22 str.size- String Length = 22 -------------------------------