1// string::length
2#include <iostream>
3#include <string>
4
5int main ()
6{
7 std::string str ("Test string");
8 std::cout << "The size of str is " << str.length() << " bytes.\n";
9 return 0;
10}
1#include <iostream>
2#include <string>
3
4int main()
5{
6 string str = "iftee";
7
8 //method 1: using length() function
9 int len = str.length();
10 cout << "The String Length: " << len << endl;
11
12 //method 2: using size() function
13 int len2 = str.size();
14 cout << "The String Length: " << len2 << endl;
15
16 return 0;
17}
1#include <string>
2#include <iostream>
3
4int main()
5{
6 std::string s(21, '*');
7
8 std::cout << s << std::endl;
9
10 return 0;
11}
12
1
2 string txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
3cout << "The length of the txt
4 string is: " << txt.length();
5
6//Tip: You might see some C++ programs that use the size() function to get the length of a string. This is just an alias of length().
7//It is completely up to you if you want to use length() or size():
8
9 string txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
10cout << "The length of the txt string is: " << txt.size();
1#include <iostream>
2#include <cstring>
3using namespace std;
4
5int main() {
6
7 // initialize C-string
8 char song[] = "We Will Rock You!";
9
10 // print the length of the song string
11 cout << strlen(song);
12
13 return 0;
14}
15
16// Output: 17