1#include<iostream>
2#include<cstring>
3using namespace std;
4main() {
5 string myStr = "This is a sample string";
6 char myStrChar[] = "This is a sample string";
7 cout << "String length using string::length() function: " << myStr.length() <<endl;
8 cout << "String length using string::size() function: " << myStr.size() <<endl;
9 cout << "String length using strlen() function for c like string: " << strlen(myStrChar) <<endl;
10 cout << "String length using while loop: ";
11 char *ch = myStrChar;
12 int count = 0;
13 while(*ch != '\0'){
14 count++;
15 ch++;
16 }
17 cout << count << endl;
18 cout << "String length using for loop: ";
19 count = 0;
20 for(int i = 0; myStrChar[i] != '\0'; i++){
21 count++;
22 }
23 cout << count;
24}