5 Different Methods to Find Length of a String in C++ Last Updated : 19 Jun, 2023 Comments Improve Suggest changes Like Article Like Report The string is a sequence of characters or an array of characters. The declaration and definition of the string using an array of chars are similar to the declaration and definition of an array of any other data type. Examples: Input: "Geeksforgeeks" Output: 13 Input: "Geeksforgeeks \0 345" Output: 14Important PointsThe constructor of the String class will set it to the C++ style string, which ends at the '\0'.The size() function is consistent with other STL containers (like vector, map, etc.), and length() is consistent with most people's intuitive notion of character strings like a word, sentence, or paragraph. We say a paragraph's length, not its size, so length() is to make things more readable.Methods to Find the Length of a String There are few methods to find the length of a string is mentioned below: Using string::sizeUsing string::length: Using the C library function strlen() method: Using while loop: Using for loop: 1. Using string::size The method string::size returns the length of the string, in terms of bytes. Below is the implementation of the above method: C++ // C++ program to find length // of a string #include <iostream> #include <string.h> using namespace std; // Driver code int main() { // String obj string str = "GeeksforGeeks"; // size of string object using size() method cout << str.size() << endl; return 0; } Output132. Using string::length The method string::length returns the length of the string, in terms of bytes. Both string::size and string::length are synonyms and return the exact same value. Below is the implementation of the above method: C++ // C++ program to find length // of a string #include <iostream> #include <string.h> using namespace std; // Driver code int main() { // String obj string str = "GeeksforGeeks"; // size of string object using length method cout << str.length() << endl; return 0; } Output133. Using strlen() Method The C library function size_t strlen(const char *str) computes the length of the string str up to, but not including the terminating null character. Below is the implementation of the above method: C++ // C++ program to find length // of a string #include <iostream> #include <string.h> using namespace std; // Driver code int main() { // String obj string str = "GeeksforGeeks"; // size using old style // size of string object using strlen function cout << strlen(str.c_str()) << endl; return 0; } Output134. Using a while loop Using the traditional method, initialize the counter equals 0 and increment the counter from starting of the string to the end of the string (terminating null character). Below is the implementation of the above method: C++ // C++ program to find length // of a string #include <iostream> #include <string.h> using namespace std; // Driver code int main() { // String obj string str = "GeeksforGeeks"; // The constructor of string will set it to the // C-style string, // which ends at the '\0' // size of string object Using while loop // while 'NOT NULL' int i = 0; while (str[i]) i++; cout << i << endl; return 0; } Output135. Using Loop To initialize the counter equals 0 and increment the counter from starting of the string to the end of the string (terminating null character). Below is the implementation of the above method: C++ // C++ program to find length // of a string #include <iostream> #include <string.h> using namespace std; // Driver code int main() { int i; // String obj string str = "GeeksforGeeks"; // The constructor of string will set it to the // C-style string, // which ends at the '\0' // size of string object using for loop // for(; NOT NULL for (i = 0; str[i]; i++); cout << i << endl; return 0; } Output13The complexity of the method above: Time complexity: For all the methods, the time complexity is O(n) as we need to traverse the entire string to find its length. Space complexity: For all the methods, the space complexity is O(1) as no extra space is required. Comment More infoAdvertise with us Next Article 5 Different Methods to Find Length of a String in C++ kartik Follow Improve Article Tags : Misc Strings C++ DSA cpp-string +1 More Practice Tags : CPPMiscStrings Similar Reads Different ways to access characters in a given String in C++ String class stores the characters as a sequence of bytes with the functionality of allowing access to the single-byte character. There are several ways to access substrings and individual characters of a string. The string class supports the following functions for this purpose: operator[]at()subst 4 min read Length Of Last Word in a String Given a string s consisting of upper/lower-case alphabets and empty space characters ' ', return the length of the last word in the string. If the last word does not exist, return 0. Examples: Input : str = "Geeks For Geeks"Output : 5length(Geeks)= 5Input : str = "Start Coding Here"Output : 4length( 13 min read Converting Number to String in C++ In C++, converting integers to strings or converting numbers to strings or vice-versa is actually a big paradigm shift in itself. In general or more specifically in competitive programming there are many instances where we need to convert a number to a string or string to a number. Let's look at som 4 min read Extract all integers from string in C++ Given a string, extract all integers words from it. Examples : Input : str = "geeksforgeeks 12 13 practice" Output : 12 13 Input : str = "1: Prakhar Agrawal, 2: Manish Kumar Rai, 3: Rishabh Gupta" Output : 1 2 3 Input : str = "Ankit sleeps at 4 am." Output : 4 The idea is to use stringstream:, objec 2 min read LEX program to print the longest string and to find average of given numbers Lex :The Lex program has purpose of generating lexical analyzer. Lex analyzers are a program that transform input streams into sequence of tokens. A C program implements the lexical analyzer to read input stream and produce output as the source code. Commands to be used - lex file_name.l // To produ 2 min read Program to find Smallest and Largest Word in a String Given a string, find the minimum and the maximum length words in it. Examples: Input : "This is a test string"Output : Minimum length word: a Maximum length word: stringInput : "GeeksforGeeks A computer Science portal for Geeks"Output : Minimum length word: A Maximum length word: GeeksforGeeksMethod 15+ min read Check length of a string is equal to the number appended at its last Given a string that (may) be appended with a number at last. You need to find whether the length of string excluding that number is equal to that number. For example for "helloworld10", answer is True as helloworld consist of 10 letters. Length of String is less than 10, 000. Examples : Input: str = 11 min read How to Convert Vector of int into String? In C++, there are numerous cases where a vector of integers is needs to be converted into a string. In this article, we will learn the different methods to convert the vector of int into string in C++.The simplest method to convert the vector of int into string is by creating a stringstream and addi 3 min read std::string::length, std::string::capacity, std::string::size in C++ STL Prerequisite: String in C++ String class is one of the features provided by the Standard template library to us, So it comes up with great functionality associated with it. With these Functionalities, we can perform many tasks easily. Let's see a few of the functionalities string class provides. Hea 6 min read Total length of string from given Array of strings composed using given characters Given a list of characters and an array of strings, find the total length of all strings in the array of strings that can be composed using the given characters.Examples: Input: string = ["mouse", "me", "bat", "lion"], chars = "eusamotb" Output: 10 Explanation: The strings that can be formed using t 6 min read Like