Popular Tutorials
Start Learning C++Certification Courses
Created with over a decade of experience and thousands of feedback.
Python online compiler
JavaScript online compiler
SQL online compiler
Java online compiler
HTML online compiler
C online compiler
C++ online compiler
C# online compiler
PHP online compiler
Swift online compiler
Kotlin online compiler
TypeScript online compiler
Go online compiler
Rust online compiler
Scala online compiler
Dart online compiler
R online compiler
Ruby online compiler
C++ Program to Find the Length of a String
In this example, you will learn to compute the length (size) of a string (both string objects and C-style strings).
To understand this example, you should have the knowledge of the following C++ programming topics:
You can get the length of a string object by using a size() function or a length() function.
The size() and length() functions are just synonyms and they both do exactly same thing.
Example: Length of String Object
#include <iostream>
using namespace std;
int main() {
string str = "C++ Programming";
// you can also use str.length()
cout << "String Length = " << str.size();
return 0;
}
Output
String Length = 15
Example: Length of C-style string
To get the length of a C-string string, strlen() function is used.
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char str[] = "C++ Programming is awesome";
cout << "String Length = " << strlen(str);
return 0;
}
Output
String Length = 26
Also Read:
Did you find this article helpful?