VOOZH about

URL: https://www.geeksforgeeks.org/cpp/how-to-extract-a-substring-from-a-character-array-in-cpp/

⇱ How to Extract a Substring from a Character Array in C++? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Extract a Substring from a Character Array in C++?

Last Updated : 23 Jul, 2025

In C++, character arrays are used to store sequences of characters also known as strings. A substring is a part of a string that consists of a continuous sequence of characters from the string. In this article, we will learn how to extract a substring from a character array in C++.

Extract a Substring from a Character Array in C++

To extract a substring from a character array in C++ follow the below approach:

Approach

  1. Convert the character array to std::string.
  2. Declare a temporary string to store the substring.
  3. Use the std::substring method to extract the substring.
  4. Store the substring in the temporary string.

C++ Program to Extract a Substring from a Character Array

The following program illustrates how we can extract a substring from a character array in C++:


Output
String: Geeks for Geeks
Substring: Geeks

Time Complexity: O(N) where N is the length of the character array.
Auxiliary Space: O(N)



Comment
Article Tags: