
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Find the Length of a String
A string is a one dimensional character array that is terminated by a null character. The length of a string is the number of characters in the string before the null character.
For example.
char str[] = “The sky is blue”; Number of characters in the above string = 15
A program to find the length of a string is given as follows.
Example
#include<iostream> using namespace std; int main() { char str[] = "Apple"; int count = 0; while (str[count] != '\0') count++; cout<<"The string is "<<str<<endl; cout <<"The length of the string is "<<count<<endl; return 0; }
Output
The string is Apple The length of the string is 5
In the above program, the count variable is incremented in a while loop until the null character is reached in the string. Finally the count variable holds the length of the string. This is given as follows.
while (str[count] != '\0') count++;
After the length of the string is obtained, it is displayed on screen. This is demonstrated by the following code snippet.
cout<<"The string is "<<str<<endl; cout<<"The length of the string is "<<count<<endl;
The length of the string can also be found by using the strlen() function. This is demonstrated in the following program.
Example
#include<iostream> #include<string.h> using namespace std; int main() { char str[] = "Grapes are green"; int count = 0; cout<<"The string is "<<str<<endl; cout <<"The length of the string is "<<strlen(str); return 0; }
Output
The string is Grapes are green The length of the string is 16
- Related Articles
- C program to find the length of a string?
- Golang Program to find the length of a string
- PHP program to find the length of the last word in the string
- Program to find length of longest repeating substring in a string in Python
- How to find the length of a string in TypeScript?
- Write a program in Python to find the maximum length of a string in a given Series
- Program to find length of concatenated string of unique characters in Python?
- Program to find length of longest valid parenthesis from given string in Python
- Program to find minimum length of string after deleting similar ends in Python
- 5 Different methods to find the length of a string in C++?
- Python Program to Calculate the Length of a String Without Using a Library Function
- Python program to find the length of the largest consecutive 1's in Binary Representation of a given string.
- Python Program to Find the Length of a List Using Recursion
- Python - Find the length of the last word in a string
- C++ Program to find if the given string has Repeated Subsequence of Length 2 or More

Advertisements