c++ program to find length string an char* 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 &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;
    |                      ^~~
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 &lt;cstring&gt; (or &lt;string.h&gt; 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 &lt; 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 &lt;&lt; my_name.length() &lt;&lt; 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 &lt;string&gt;.



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 &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;
    }
}


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.