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: "Geeksforneveropen" Output: 13 Input: "Geeksforneveropen \0 345" Output: 14
Important Points
- The 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::size
- Using 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 = "neveropen" ; Ā
Ā Ā Ā Ā // size of string object using size() method Ā Ā Ā Ā cout << str.size() << endl; Ā
Ā Ā Ā Ā return 0; } |
13
2. 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 = "neveropen" ; Ā
Ā Ā Ā Ā // size of string object using length method Ā Ā Ā Ā cout << str.length() << endl; Ā
Ā Ā Ā Ā return 0; } |
13
3. 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 = "neveropen" ; Ā
Ā Ā Ā Ā // size using old style Ā Ā Ā Ā // size of string object using strlen function Ā Ā Ā Ā cout << strlen (str.c_str()) << endl; Ā
Ā Ā Ā Ā return 0; } |
13
4. 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 = "neveropen" ; Ā
Ā Ā Ā Ā // 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; } |
13
5. 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 = "neveropen" ; Ā
Ā Ā Ā Ā // 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; } |
13
The 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.