1

I want to make a function that will do the same as Strlen does, andI get the error: "string subscript out of range". I tried fixing it but has no idea.

heres the code:

#include "stdafx.h"
#include "stdafx.h"
#include <string>
#include <sstream>
#include <iostream>


using namespace std;

int strlen1( string str)
{
    int count=0;
    int i=0;
    while (str[i]!='\0')
        {
            count++;
            i++;
    }
    return count;
}

int _tmain(int argc, _TCHAR* argv[])
{
    string input;
    cin >> input;
    cout << strlen1(input) << endl;

    return 0;
}

thanks!

Aviran Cohen
  • 5,581
  • 4
  • 48
  • 75

4 Answers4

4

In short, for a non-const std::stringaccessing an element beyond the string length has Undefined Behavior (more precisely, when using the non-const operator[] for that).

C++98 §21.3.4/1, about std::string::operator[]:

If pos < size, returns data()[pos]. Otherwise, if pos == size(), the const version returns charT(). Otherwise the behavior is undefined.

And your string is non-const.

You can make it const, which will then invoke a special provision in the standard that guarantees zero result for element n, but that does not address the problem that your function is completely redundant for std::string.

Perhaps you mean to have argument type char const*.

That would make more sense, and then also the function would work.

Cheers & hth.,

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • the const made it work, but as you said i think that the right solution is to have argument type char* instead of string. unfortunately, when i replaced it in str and input variables it didnt work saying the variable 'input' is being used without being intialized. i have no idea how to make it work – Aviran Cohen Feb 03 '11 at 20:03
  • Just change the `str` variable to a `const char *`; leave `input` as a `string`. Then call it like `strlen1(input.c_str())`. – Daniel Gallagher Feb 03 '11 at 20:10
  • Never knew it. Apparently in C++0x `s[s.size()]` will always return a reference to a NUL character (and you must not modify the value). – UncleBens Feb 04 '11 at 00:05
3

You seem to be under the misconception that std::string is the same as a C-style string (char const*). This is simply not true. There is no null terminator.

Here's your strlen function the only way it will work:

int strlen1(string str) { return static_cast<int>(str.size()); }
Edward Strange
  • 40,307
  • 7
  • 73
  • 125
1

std::string has a nice size() method for this.

Nikolai Fetissov
  • 82,306
  • 11
  • 110
  • 171
  • are you sure you have to create a strlenb function that takes a std::string as an argument, not a `char*` ? – kriss Feb 03 '11 at 19:36
  • you right, i've made mistake, need it as char*. unfortunately, when i replaced it in str and input variables it didnt work saying the variable 'input' is being used without being intialized. i have no idea how to make it work – Aviran Cohen Feb 03 '11 at 20:04
0

strlen is based on zero terminated strings (char*, or char arrays), C++ string are based on length + data, you can't access to data after the end of string (where a terminal zero would be). Beside that, getting the length is a standard property of C++ strings (size()), hence the standard way of writing strlen is just to call size.

kriss
  • 23,497
  • 17
  • 97
  • 116
  • 1
    Actually, for a *constant* `std::string` of length *n*, accessing element *n* (which is beyond the string length) is required to yield `'\0'`. It's a special case provision in the standard. So if the OP had just added a little `const` to the formal argument type the `strlen1` function would have been technically OK. – Cheers and hth. - Alf Feb 03 '11 at 19:35
  • what do you mean sero teminated strings? and how do you think i can create a strlen function by my own? – Aviran Cohen Feb 03 '11 at 19:37
  • @Aviran: I mean creating a strlen function based on std::string as input is probably one of dumber thing I ever heard of. If it's some homework you should consider looking for a better teacher. – kriss Feb 03 '11 at 21:21
  • @Alf P. Steinbach: technically ok, you say it, and there is many ways to do the same thing that would work without const. But I agree for a function like strlen, making parameter const looks logical, I wouldn't like a strlen that is allowed to destroyed the original string to compute the length. – kriss Feb 03 '11 at 21:35