VOOZH about

URL: https://www.geeksforgeeks.org/cpp/how-to-find-length-of-substring-in-a-char-array-in-cpp/

⇱ How to Find the Length of a Substring in a char Array? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Find the Length of a Substring in a char Array?

Last Updated : 23 Jul, 2025

In C++, a substring is a string inside another string that is the contiguous part of the string. In this article, we will learn how to find the length of a substring in a char array in C++.

Example

Input: 
str[] = "Hello, World!";
substr= "World";
Output: Length of substring World is: 5

Finding Length of Substring Within Char Array in C++

To find the length of a substring in a char array iterate through the character array and use the std::strstr() function to search for the first occurrence of a substring within a character array if the substring is found calculate its length using the std::string::length() function.

C++ Program to Find Length of Substring in a Char Array

The below example demonstrates how we can find the length of the given substring in a char array.


Output
Length of the substring 'World': 5

Time Complexity: O(N), here N is the length of parent string.
Auxilliary Space: O(1)

Comment