<p>I think you're confusing C++ and C strings, which are very different. When you have a <code>char *</code>, that's a C-style "string". It's really just a bunch of bytes with a zero byte at the end, demarcating the end of the string. To find the length of a C string you include <code>&lt;cstring&gt;</code> (or <code>&lt;string.h&gt;</code> in C) and call <code>strlen</code>, like this:</p>

<pre><code>/** Print every character in the string */
void print_chars_in_string(char* word) {
    for (size_t i = 0; i &lt; strlen(word); ++i) {
        putchar(word[i]);
    }
}
</code></pre>

<p><code>strlen</code> is actually pretty simple though, you can write your own version as an academic exercise like this:</p>

<pre><code>size_t deluxe_bespoke_strlen(char* str) {
    size_t length = 0;

    while (*str++)
        ++length;

    return length;
}
</code></pre>

<p>Since the while-loop condition will be true until you hit the null terminator (the zero byte at the end), this is how you would implement the strlen function without a for-loop, otherwise you would need the strlen function for the strlen function.</p>

<p>In C++, strings are actual objects, with methods you can call. To get the length of a C++ string simply do this:</p>

<pre><code>std::string my_name = "Jose";
std::cout &lt;&lt; my_name.length() &lt;&lt; std::endl;
</code></pre>

<p>If you have a C string, you can convert it to a C++ string with the built-in constructor:</p>

<pre><code>const char* my_name = "Jose";
std::string my_name_cppstring = std::string(my_name);
</code></pre>

<p>You can also get the C version of a C++ string by calling the <code>c_str()</code> member function like this:</p>

<pre><code>std::string sequence = "GATTACA";
size_t length = strlen(sequence.c_str());
</code></pre>

<p>My recommendation is to not mix C and C++. C is my favorite programming language, but that doesn't mean everyone should write everything in C. Since you're using C++ I'm guessing that's your preference, so I would suggest using C++ strings instead of C strings, just remember to include <code>&lt;string&gt;</code>.</p>

<hr />

<p>As for your specific question, the compiler is giving you the correct message: <code>str</code> is not a member of <code>std</code>. You don't add the namespace to variables. This is how that works:</p>

<pre><code>#include &lt;cstdlib&gt;
#include &lt;iostream&gt;

namespace CustomDeluxeBespokeStandardNamespace
{
    size_t custom_bespoke_strlen(char* str)
    {
        size_t length = 0;

        while (*str++)
            ++length;

        return length;
    }
}

int main(int argc, char *argv[])
{
    if (argc == 1) {
        std::cerr &lt;&lt; "No input(s)." &lt;&lt; std::endl;
        std::exit(EXIT_FAILURE);
    }

    while (*++argv) {
        std::cout &lt;&lt; *argv &lt;&lt; ": " &lt;&lt; CustomDeluxeBespokeStandardNamespace::custom_bespoke_strlen(*argv) &lt;&lt; std::endl;
    }
}
</code></pre>

<p>So if you run it like this:</p>

<pre><code>testprogram horse house mouse cat
</code></pre>

<p>You should get this output:</p>

<pre><code>horse: 5
house: 5
mouse: 5
cat: 3
</code></pre>

<p>So to recap, you were adding the namespace to your variables, and the compiler was giving you an error because it correctly could not find anything by that name in the <code>std</code> namespace. Even if it had though, C style strings do not have member functions. There are no such things as objects in C, at least not in the sense that you can define member functions for an object, so that was the second error.</p>

<hr />

<p>As an aside, you will note all my examples using <code>strlen</code> both custom and actual return <code>size_t</code>. This is not an accident; in both C and C++ the return type of a call to <code>strlen</code> is <code>size_t</code>. You have to be careful because in your code you compare an <code>int</code> to a <code>size_t</code>. Comparing a signed value to an unsigned value is <a href="https://stackoverflow.com/questions/5416414/signed-unsigned-comparisons">Not</a> <a href="https://softwareengineering.stackexchange.com/questions/175253/why-does-an-unsigned-int-compared-with-a-signed-character-turn-out-with-an-unexp">Good</a> because it will work only if the signed value can be faithfully represented after a conversion to an unsigned value. If it can't though, you're in for a bad time.</p>

CC BY-SA 4.0
2019-08-26T11:18:41.487Z
ok, it did, but no results are shown when i create the file
fatemeh khoramdel
CC BY-SA 4.0
2019-08-26T11:23:43.093Z
Can you post your code?
Jose Fernando Lopez Fernandez
CC BY-SA 4.0
2019-08-29T05:43:03.753Z
my code have 2000 line...i think i can't post code here..
fatemeh khoramdel
CC BY-SA 4.0
2019-08-31T06:52:44.653Z
how i can post it?
fatemeh khoramdel
CC BY-SA 4.0
2019-08-31T13:33:20.550Z
Can you link to a github?
Jose Fernando Lopez Fernandez
CC BY-SA 4.0
2019-09-02T03:14:00.647Z
No,it's not possible.i'm iranian.github has boycotted iran:(
fatemeh khoramdel
CC BY-SA 4.0
2019-09-02T21:30:55.057Z
Damn, if the code doesn't contain any sensitive information you can email it to me at `jflopezfernandez@gmail.com` and I can help you over email, since the question got closed lol
Jose Fernando Lopez Fernandez
CC BY-SA 4.0
2019-09-05T08:42:58.687Z
Thanks, I sending you
fatemeh khoramdel
CC BY-SA 4.0
2019-08-24T22:31:59.940Z
2019-08-24T22:31:59.940Z
Jose Fernando Lopez Fernandez
<p>I write under code but I can't run it when I run by g++ . erro is about  strlen.<br>
errors: </p>

<pre><code>a.cpp: In function ‘bool checker(char*, int)’:
a.cpp:185:26: error: ‘str’ is not a member of ‘std’
 185 |  for(int i = 0; i &lt; std::str.length(word); i++)
  |                          ^~~
a.cpp: In function ‘int status(char*, int, int)’:
a.cpp:192:40: error: ‘str’ is not a member of ‘std’
   192 |  for(int i = 0; i &lt; 4 &amp;&amp; nw*4+i &lt; std::str.length(pmt[pn]); i++)
  |                                        ^~~
a.cpp:199:10: error: ‘str’ is not a member of ‘std’
  199 |  if(std::str.length(pmt[pn]) &gt; nw*4)
  |          ^~~
a.cpp: In function ‘int main()’:
a.cpp:225:22: error: ‘str’ is not a member of ‘std’
  225 |     len_words = std::str.length(pmt[i])/2;
    |                      ^~~
</code></pre>

CC BY-SA 4.0
2019-08-23T13:07:40.507Z
Isn't this a better question for stackoverflow?
Devon Ryan
CC BY-SA 4.0
2019-08-23T13:09:31.970Z
how i can ask best question
fatemeh khoramdel
CC BY-SA 4.0
2019-08-23T15:04:09.480Z
@fatemehkhoramdel please format your question properly, check your English to make it easier to understand (capitalize the beginning of sentences, ...). Explain what this code it is supposed to do and why it is relevant that you need bioinformaticians to help you (there are other sites to get help that might have people with the right skills to help you, but they might not be here). Good luck
llrs
CC BY-SA 4.0
2019-08-23T16:25:08.050Z
I think this is what you're looking for: [error strlen not a member of std only on linux not on solaris](//stackoverflow.com/q/18974741).
terdon
CC BY-SA 4.0
2019-08-23T19:21:39.097Z
it not usefull for me.i treid about it
fatemeh khoramdel
CC BY-SA 4.0
2019-08-23T21:36:53.103Z
I'm voting to close this question as off-topic because it belongs on Stack Overflow.
Alex Reynolds
CC BY-SA 4.0
2019-08-25T16:03:06.837Z
@AlexReynolds Respectfully, I would caution against simply closing as off-topic simply because this is a programming question. While I agree this question is not directly related to bioinformatics, the expectation of prior knowledge is going to be different for bioinformaticians and computer scientists. The SO C/C++ community is not really for beginners, and I think it would be more productive for bioinformatics to have a source for entry-level programming questions, as for them it's a tool and not the end result.
Jose Fernando Lopez Fernandez
<c++>
c++ program to find length string an char*
# c++ program to find length string an char*

(Asked by: username_0 on 2019-08-23)

I write under code but I can't run it when I run by g++ . erro is about  strlen.
errors: 
a.cpp: In function ‘bool checker(char*, int)’:
a.cpp:185:26: error: ‘str’ is not a member of ‘std’
 185 |  for(int i = 0; i < std::str.length(word); i++)
  |                          ^~~
a.cpp: In function ‘int status(char*, int, int)’:
a.cpp:192:40: error: ‘str’ is not a member of ‘std’
   192 |  for(int i = 0; i < 4 && nw*4+i < std::str.length(pmt[pn]); i++)
  |                                        ^~~
a.cpp:199:10: error: ‘str’ is not a member of ‘std’
  199 |  if(std::str.length(pmt[pn]) > nw*4)
  |          ^~~
a.cpp: In function ‘int main()’:
a.cpp:225:22: error: ‘str’ is not a member of ‘std’
  225 |     len_words = std::str.length(pmt[i])/2;
    |                      ^~~

**Tags:** c++
> **Comments:**
> **username_1**: Isn't this a better question for stackoverflow?  
> **username_0**: how i can ask best question  
> **username_2**: @fatemehkhoramdel please format your question properly, check your English to make it easier to understand (capitalize the beginning of sentences, ...). Explain what this code it is supposed to do and why it is relevant that you need bioinformaticians to help you (there are other sites to get help that might have people with the right skills to help you, but they might not be here). Good luck  
> **username_3**: I think this is what you're looking for: [error strlen not a member of std only on linux not on solaris](//stackoverflow.com/q/18974741).  
> **username_0**: it not usefull for me.i treid about it  
> **username_4**: I'm voting to close this question as off-topic because it belongs on Stack Overflow.  
> **username_5**: @AlexReynolds Respectfully, I would caution against simply closing as off-topic simply because this is a programming question. While I agree this question is not directly related to bioinformatics, the expectation of prior knowledge is going to be different for bioinformaticians and computer scientists. The SO C/C++ community is not really for beginners, and I think it would be more productive for bioinformatics to have a source for entry-level programming questions, as for them it's a tool and not the end result.  

---

## 1 answers

### Answer by username_5 on 2019-08-24. Score: 1

I think you're confusing C++ and C strings, which are very different. When you have a char *, that's a C-style "string". It's really just a bunch of bytes with a zero byte at the end, demarcating the end of the string. To find the length of a C string you include  (or  in C) and call strlen, like this:
/** Print every character in the string */
void print_chars_in_string(char* word) {
    for (size_t i = 0; i < strlen(word); ++i) {
        putchar(word[i]);
    }
}

strlen is actually pretty simple though, you can write your own version as an academic exercise like this:
size_t deluxe_bespoke_strlen(char* str) {
    size_t length = 0;

    while (*str++)
        ++length;

    return length;
}

Since the while-loop condition will be true until you hit the null terminator (the zero byte at the end), this is how you would implement the strlen function without a for-loop, otherwise you would need the strlen function for the strlen function.
In C++, strings are actual objects, with methods you can call. To get the length of a C++ string simply do this:
std::string my_name = "Jose";
std::cout << my_name.length() << std::endl;

If you have a C string, you can convert it to a C++ string with the built-in constructor:
const char* my_name = "Jose";
std::string my_name_cppstring = std::string(my_name);

You can also get the C version of a C++ string by calling the c_str() member function like this:
std::string sequence = "GATTACA";
size_t length = strlen(sequence.c_str());

My recommendation is to not mix C and C++. C is my favorite programming language, but that doesn't mean everyone should write everything in C. Since you're using C++ I'm guessing that's your preference, so I would suggest using C++ strings instead of C strings, just remember to include .

As for your specific question, the compiler is giving you the correct message: str is not a member of std. You don't add the namespace to variables. This is how that works:
#include 
#include 

namespace CustomDeluxeBespokeStandardNamespace
{
    size_t custom_bespoke_strlen(char* str)
    {
        size_t length = 0;

        while (*str++)
            ++length;

        return length;
    }
}

int main(int argc, char *argv[])
{
    if (argc == 1) {
        std::cerr << "No input(s)." << std::endl;
        std::exit(EXIT_FAILURE);
    }

    while (*++argv) {
        std::cout << *argv << ": " << CustomDeluxeBespokeStandardNamespace::custom_bespoke_strlen(*argv) << std::endl;
    }
}

So if you run it like this:
testprogram horse house mouse cat

You should get this output:
horse: 5
house: 5
mouse: 5
cat: 3

So to recap, you were adding the namespace to your variables, and the compiler was giving you an error because it correctly could not find anything by that name in the std namespace. Even if it had though, C style strings do not have member functions. There are no such things as objects in C, at least not in the sense that you can define member functions for an object, so that was the second error.

As an aside, you will note all my examples using strlen both custom and actual return size_t. This is not an accident; in both C and C++ the return type of a call to strlen is size_t. You have to be careful because in your code you compare an int to a size_t. Comparing a signed value to an unsigned value is Not Good because it will work only if the signed value can be faithfully represented after a conversion to an unsigned value. If it can't though, you're in for a bad time.

> **Comments:**
> **username_0**: ok, it did, but no results are shown when i create the file  
> **username_5**: Can you post your code?  
> **username_0**: my code have 2000 line...i think i can't post code here..  
> **username_0**: how i can post it?  
> **username_5**: Can you link to a github?  
> **username_0**: No,it's not possible.i'm iranian.github has boycotted iran:(  
> **username_5**: Damn, if the code doesn't contain any sensitive information you can email it to me at `jflopezfernandez@gmail.com` and I can help you over email, since the question got closed lol  
> **username_0**: Thanks, I sending you  

---

