Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get length of a string using strlen function

Tags:

c++

string

I have following code that gets and prints a string.

#include<iostream> #include<conio.h> #include<string> using namespace std;  int main() {     string str;     cout << "Enter a string: ";     getline(cin, str);     cout << str;     getch();     return 0; } 

But how to count the number of characters in this string using strlen() function?

like image 937
Akash Sharma Avatar asked Nov 24 '13 20:11

Akash Sharma


People also ask

How do you find a length of a string using strlen function?

C strlen() The strlen() function calculates the length of a given string. The strlen() function takes a string as an argument and returns its length. The returned value is of type size_t (an unsigned integer type). It is defined in the <string.

How do you calculate the length of a string?

As you know, the best way to find the length of a string is by using the strlen() function.

How do you find the length of a string input?

In this java program, to find the length of a string we will use length() method. This method returns the length of this string which is equal to the number of characters in the string.

What is the difference between strlen () and sizeof () with example?

sizeof() vs strlen() vs size() Data Types Supported: sizeof() gives the actual size of any type of data (allocated) in bytes (including the null values), strlen() is used to get the length of an array of chars/string whereas size() returns the number of the characters in the string.


1 Answers

For C++ strings, there's no reason to use strlen. Just use string::length:

std::cout << str.length() << std::endl; 

You should strongly prefer this to strlen(str.c_str()) for the following reasons:

  1. Clarity: The length() (or size()) member functions unambiguously give back the length of the string. While it's possible to figure out what strlen(str.c_str()) does, it forces the reader to pause for a bit.

  2. Efficiency: length() and size() run in time O(1), while strlen(str.c_str()) will take Θ(n) time to find the end of the string.

  3. Style: It's good to prefer the C++ versions of functions to the C versions unless there's a specific reason to do so otherwise. This is why, for example, it's usually considered better to use std::sort over qsort or std::lower_bound over bsearch, unless some other factors come into play that would affect performance.

The only reason I could think of where strlen would be useful is if you had a C++-style string that had embedded null characters and you wanted to determine how many characters appeared before the first of them. (That's one way in which strlen differs from string::length; the former stops at a null terminator, and the latter counts all the characters in the string). But if that's the case, just use string::find:

size_t index = str.find(0); if (index == str::npos) index = str.length(); std::cout << index << std::endl; 

Hope this helps!

like image 57
templatetypedef Avatar answered Oct 02 '22 05:10

templatetypedef