The string is a sequence of characters or an array of characters. The declaration and definition of the string using an array of chars is similar to declaration and definition of an array of any other data type.
- The constructor of 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 peoples intuitive notion of character strings like a word, sentence or paragraph. We say a paragraph’ss length not its size, so length() is to make things more readable.
Methods to find length of string
- Using string::size: The method string::size returns the length of the string, in terms of bytes.
- 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.
- Using C library function 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.
- Using while loop: Using the traditional method, To initialize the counter equals 0 and increment the counter from starting of string to end of string (terminating null character).
- Using for loop: To initialize the counter equals 0 and increment the counter from starting of string to end of string (terminating null character).
Input: "Geeksforgeeks" Output: 13 Input: "Geeksforgeeks\0 345" Output: 13 Input: "Geeksforgeeks \0 345" Output: 14
13 13 13 13 13
This article is contributed by Prakhar Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Different methods to reverse a string in C/C++
- Different methods to copy in C++ STL | std::copy(), copy_n(), copy_if(), copy_backward()
- Methods to concatenate string in C/C++ with Examples
- Hiding of all overloaded methods with same name in base class
- Calling virtual methods in constructor/destructor in C++
- C++: Methods of code shortening in competitive programming
- Default Methods in C++ with Examples
- Implementation of file allocation methods using vectors
- Implementation of all Partition Allocation Methods in Memory Management
- Methods vs. Functions in C++ with Examples
- Counts of distinct consecutive sub-string of length two using C++ STL
- Find words which are greater than given length k using stringstream
- Write a program that produces different results in C and C++
- Print 2D matrix in different lines and without curly braces in C/C++?
- Array of Strings in C++ (5 Different Ways to Create)
- Different ways to delete elements in std::map (erase() and clear())
- Namespaces in C++ | Set 4 (Overloading, and Exchange of Data in different Namespaces)
- Different ways to declare variable as constant in C and C++
- Initialize a vector in C++ (5 different ways)
- Preventing Object Copy in C++ (3 Different Ways)
Improved By : abhishek363