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
1string str ="hello world";
2//different ways to find length of a string:
3str.length();
4str.size();
5
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