Sunday, December 29, 2024
Google search engine
HomeData Modelling & AI5 Different Methods to Find Length of a String in C++

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

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

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


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


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.

Whether youā€™re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions weā€™ve already empowered, and weā€™re here to do the same for you. Donā€™t miss out ā€“ check it out now!

RELATED ARTICLES

Most Popular

Recent Comments

ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
źøˆģ²œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
źµ¬ģ›”ė™ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ˜¤ģ‚°ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ•ˆģ–‘ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė™ķƒ„ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ„œģšøģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶„ė‹¹ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ź³”ė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź³ ģ–‘ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ģ„±ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ²œķ˜øė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?