Open In App

5 Different Methods to Find Length of a String in C++

Improve
Improve
Like Article
Like
Save
Share
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: 14

Important Points

  1. The constructor of the String class will set it to the C++ style string, which ends at the ‘\0‘.
  2. 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 = "GeeksforGeeks";
 
    // size of string object using size() method
    cout << str.size() << endl;
 
    return 0;
}


Output

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 = "GeeksforGeeks";
 
    // size of string object using length method
    cout << str.length() << endl;
 
    return 0;
}


Output

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 = "GeeksforGeeks";
 
    // size using old style
    // size of string object using strlen function
    cout << strlen(str.c_str()) << endl;
 
    return 0;
}


Output

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 = "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;
}


Output

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 = "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;
}


Output

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.



Last Updated : 19 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads