1#include <cstring>
2#include <cstring>
3
4using namespace std;
5
6int main(void) {
7 char stringName[] = "This contains the content of your string";
8 int stringLength = strlen(stringName);
9 cout << "String: " << stringName << endl << "Length of String: " << stringLength << endl;
10
11 return 0;
12}
1#include <iostream>
2#include <string.h>
3
4using namespace std;
5
6int main()
7{
8 char *str = "ABC";
9 cout << strlen(str) << endl;
10 return 0;
11}
1#include <cstring>
2#include <iostream>
3
4using namespace std;
5
6int main()
7{
8 char str1[] = "This a string";
9
10 int len1 = strlen(str1);
11
12 cout << "Length of str1 = " << len1 << endl;
13
14 return 0;
15}
1#include <cstring>
2#include <iostream>
3using namespace std;
4
5int main() {
6 char str1[] = "This a string";
7 char str2[] = "This is another string";
8
9 // find lengths of str1 and str2
10 // size_t return value converted to int
11 int len1 = strlen(str1);
12 int len2 = strlen(str2);
13
14 cout << "Length of str1 = " << len1 << endl;
15 cout << "Length of str2 = " << len2 << endl;
16
17 if (len1 > len2)
18 cout << "str1 is longer than str2";
19 else if (len1 < len2)
20 cout << "str2 is longer than str1";
21 else
22 cout << "str1 and str2 are of equal length";
23
24 return 0;
25}
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