4

I have been trying to find the length of string which has an array of chars with strlen() function but it is not working. The code I am using is something like this:

string s[]={"a","b","c"};
int len = strlen(s.c_str());

It produces the following error:

"request for member âc_strâ in âwArrayâ, which is of non-class type"

But when I have used this strlen() function on strings before like this, it worked fine:

fin.open("input.txt");
string tempStr;
getline(fin, tempStr,'\n');
int len = strlen(tempStr.c_str());

What I am missing here? I know I can find the length of string s[] with

int size = sizeof( s ) / sizeof( s[ 0 ] );

But why can't I use strlen(). Can someone explain what is going on?

7
  • 3
    s is an array, it has no c_str function. is std::vector an option? Commented Feb 17, 2013 at 8:25
  • You are missing the fact that std::string is not std::string[]. Commented Feb 17, 2013 at 8:27
  • @Billz But it works for char type array....???????????/ Commented Feb 17, 2013 at 8:29
  • @n.m. Then why does it work on tempStr. That is a string as well. Commented Feb 17, 2013 at 8:31
  • 1
    a string is different from an array of strings. Commented Feb 17, 2013 at 8:32

4 Answers 4

8

Finding the length of a fixed size array of any type is easy enough with a helper function template:

#include <cstddef> // for std::size_t

template< class T, size_t N >
std::size_t length(const T (&)[N] )
{
  return N;
};

string s[]={"a","b","c"};
std::cout << length(s) << std::endl;

In C++11, the function would be constexpr.

Concerning strlen, it counts chars until it finds a null termination character \0. When you call std::string'sc_str() method, you get a pointer to the first char in a null terminated string.

Sign up to request clarification or add additional context in comments.

3 Comments

@juanchopanza Thanks.. But i really need to know why strlen() works on tempStr and not string s. (see code for tempStr and s)
@AlexChamberlain I didn't remember off the top of my head which header is required for std::size_t. Fixed now.
@DeadCoder I added something about strlen.
1

A C++ way to do it would be using std::vector

vector<string> ss;

ss.push_back("a");
ss.push_back("b");
ss.push_back("c");

cout << ss.size(); // number of strings in your vector : 3

you can also access each string :

cout << ss[0].size(); // 1

Comments

0

it must be like :

string s[]={"a","b","c"};  //array of string
for(auto i:s){
int len = strlen(i.c_str());  because you need to convert one string to c type string
cout<<len<<" ";
}

to get the array length :

std::array<int> nums {1, 3, 5, 7};

std::cout << "nums contains " << nums.size() << " elements.\n";

http://en.cppreference.com/w/cpp/container/array/size

strlen() accepts ctype strings. and c_str() convert the string(STL) to ctype string(Null terminated) which is accepted by strlen.

7 Comments

@Arpit of course it is 1, Every element of the array has length 1.
so this is what to be expected.
@juanchopanza we understand the ques. differently . you work on array and i work on individual string.
Sorry, that comment was meant to be for @DeadCoder.
@juanchopanza Just last thing. What is difference between ctype string and string ????
|
0

the define of strlen: size_t strlen ( const char * str ), note the type of input parameter. you define string s[]={"a","b","c"}; which is an array, so the following statement will work: strlen(s[0].c_str()); In addition, to calculate the length of array s, sizeof( s ) / sizeof( s[ 0 ] ) work only when every element of 's' has the same length.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.