How to Extract a Substring from a Character Array in C++? Last Updated : 23 Jul, 2025 Suggest changes Share Like Article Like Report 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: ApproachConvert the character array to std::string.Declare a temporary string to store the substring.Use the std::substring method to extract the substring.Store the substring in the temporary string.C++ Program to Extract a Substring from a Character ArrayThe following program illustrates how we can extract a substring from a character array in C++: C++ // C++ Program to illustrate how to extract a substring from // a character array #include <iostream> #include <string> using namespace std; int main() { // Declare the character array char arr[] = "Geeks for Geeks"; // Convert the character array to string string str(arr); // Print the string cout << "String: " << str << endl; // Extract the substring starting at index 0 which is of // length 5. string sub = str.substr(0, 5); // Print the substring cout << "Substring: " << sub << endl; return 0; } OutputString: Geeks for Geeks Substring: Geeks Time Complexity: O(N) where N is the length of the character array.Auxiliary Space: O(N) A am8254s3a Follow 0 Article Tags : C++ Programs C++ cpp-string CPP Examples Explore C++ BasicsIntroduction to C++3 min readData Types in C++6 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++3 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++12 min readFile Handling in C++8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++6 min readPolymorphism in C++5 min readEncapsulation in C++3 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL2 min readIterators in C++ STL10 min readC++ STL Algorithm Library3 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples4 min read My Profile ${profileImgHtml} My Profile Edit Profile My Courses Join Community Transactions Logout Like