How to Find the Length of a Substring in a char Array? Last Updated : 01 Mar, 2024 Comments Improve Suggest changes 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 Largest substring where all characters appear at least K times | Set 2 Given a string str and an integer K, the task is to find the length of the longest sub-string S such that every character in S appears at least K times.Examples:Input: str = "aabbba", K = 3Output: 6 Explanation: In substring aabbba, each character repeats at least k times and its length is 6.Input: 7 min read Count of sub-strings of length n possible from the given string Given a string str and an integer N, the task is to find the number of possible sub-strings of length N.Examples: Input: str = "geeksforgeeks", n = 5 Output: 9 All possible sub-strings of length 5 are "geeks", "eeksf", "eksfo", "ksfor", "sforg", "forge", "orgee", "rgeek" and "geeks".Input: str = "jg 6 min read Longest substring that starts with X and ends with Y Given a string str, two characters X and Y. The task is to find the length of the longest substring that starts with X and ends with Y. It is given that there always exists a substring that starts with X and ends with Y. Examples: Input: str = "QWERTYASDFZXCV", X = 'A', Y = 'Z' Output: 5 Explanation 10 min read Count unique substrings of a string S present in a wraparound string Given a string S which is an infinite wraparound string of the string "abcdefghijklmnopqrstuvwxyz", the task is to count the number of unique non-empty substrings of a string p are present in s. Examples: Input: S = "zab"Output: 6Explanation: All possible substrings are "z", "a", "b", "za", "ab", "z 12 min read std::string::rfind in C++ with Examples The std::string::rfind is a string class member function that is used to search the last occurrence of any character in the string. If the character is present in the string then it returns the index of the last occurrence of that character in the string else it will return string::npos which denote 5 min read Like