How to Find the Length of a Substring in a char Array? Last Updated : 01 Mar, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: 5Finding 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 ArrayThe below example demonstrates how we can find the length of the given substring in a char array. C++ // C++ Program to find length of a substring in a char array #include <cstring> // For strstr #include <iostream> #include <string> // For std::string using namespace std; // Function to find the length of a substring within a char // array int findLen(const char* str, const string& substr) { // Search for the substring in the char array const char* found = strstr(str, substr.c_str()); if (found != nullptr) { // If found, return the length of the substring return substr.length(); } else { // If not found, return -1 return -1; } } int main() { // Example usage const char* str = "Hello, World!. This is GeeksforGeeks"; string substr = "World"; // Find the length of the substring int length = findLen(str, substr); // Output the result if (length != -1) { cout << "Length of the substring '" << substr << "': " << length << endl; } else { cout << "Substring not found." << endl; } return 0; } OutputLength of the substring 'World': 5 Time Complexity: O(N), here N is the length of parent string.Auxilliary Space: O(1) Comment More infoAdvertise with us Next Article How to Find the Length of a Substring in a char Array? M maha123 Follow Improve Article Tags : C++ Programs C++ cpp-array CPP Array and String substring CPP Examples +2 More Practice Tags : CPP Similar Reads How to Check if a Substring Exists in a Char Array in C++? In C++, Char array and std::string both are used to store a sequence of characters to represent textual data. In this article, we will learn how to check if a substring exists within a char array in C++. Example Input: charArray[]= "Hello, Geek" subString="Geek" Output: Geek substring is found in ch 2 min read How to Compare Two Substrings in a Character Array in C++? In C++, character arrays are used to store a sequence of characters also known as strings. A Substring is a continuous sequence of characters within a string. In this article, we will learn how we can compare two substrings in a character array in C++. Examples: Input: string: "Hello World" Substrin 2 min read How to Extract a Substring from a Character Array in C++? 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 Substri 2 min read C++ Program To Find Length Of The Longest Substring Without Repeating Characters Given a string str, find the length of the longest substring without repeating characters. For âABDEFGABEFâ, the longest substring are âBDEFGAâ and "DEFGAB", with length 6.For âBBBBâ the longest substring is âBâ, with length 1.For "GEEKSFORGEEKS", there are two longest substrings shown in the below 6 min read Abbreviate given string by replacing all characters with length except the first and last Given string str, the task is to convert the given string into its abbreviation of the form: first character, number of characters between first and last character, and the last character of the string. Examples: Input: str = "internationalization"Output: i18nExplanation: First letter 'i', followed 3 min read Like