5 Different methods to find length of a string in C++ - GeeksforGeeks

16 Jan, 2020
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.
Important points:
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.
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).
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).
Examples:
Input: "Geeksforgeeks"
Output: 13

Input: "Geeksforgeeks\0 345"
Output: 13

Input: "Geeksforgeeks \0 345"
Output: 14

// CPP program to illustrate// Different methods to find length// of a string#include <iostream>#include <string.h>using namespace std;int main(){    // String obj    string str = "GeeksforGeeks";      // 1. size of string object using size() method    cout << str.size() << endl;      // 2. size of string object using length method    cout << str.length() << endl;      // 3. size using old style    // size of string object using strlen function    cout << strlen(str.c_str()) << endl;      // The constructor of string will set it to the    // C-style string,    // which ends at the '\0'      // 4. size of string object Using while loop    // while 'NOT NULL'    int i = 0;    while (str[i])        i++;    cout << i << endl;      // 5. size of string object using for loop    // for(; NOT NULL     for (i = 0; str[i]; i++)        ;    cout << i << endl;      return 0;}
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 contribute@geeksforgeeks.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.
absh2702
cpp-string
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
C++ Classes and Objects
Constructors in C++
Socket Programming in C/C++
Operator Overloading in C++
Multidimensional Arrays in C / C++
Copy Constructor in C++
vector erase() and clear() in C++
Virtual Function in C++
Templates in C++ with Examples
unordered_map in C++ STL