4

I'm trying to convert my basic C code to C++ using the C++ syntax instead of C syntax, if that makes sense. However, I have a problem. I don't know how to use strlen() in C++. In preprocessing, I have #include <iostream> #include <string> and using namespace std;. When I try to compile, it gave the following error messages:

error: use of undeclared identifier 'strlen'
int n = strlen(MessagetEnc);

and

error: use of undeclared identifier 'strlen'
for (int i = 0; i < strlen(MessagetEnc); i++)

Also, using #include <cstring> doesn't seem to fix the problem.

This is the code:

#include <iostream>
#include <string>
using namespace std;
    
int main () 
{
    int EncCode; 
    std::cout << "Encryption code: " << std::endl;
    std::cin >> EncCode; 
    
    string MessagetEnc;
    std::cout << "Message to Encrypt:";
    std::cin >> MessagetEnc;
    std::cout << "Output: " << endl;
    
    int n = strlen(MessagetEnc);
    for (int i = 0; i < strlen(MessagetEnc); i++)
    {
        std::cout <<"Encrypted message" << MessagetEnc[i];
    }
}

I know C++ isn't beginner-friendly, I just wanted to try it after reading a few articles, as I plan to fully learn it after I leave the "beginner stage."

Edit: std:: is there because I tried getting rid of using namespace std; as a way to debug.

Raedwald
  • 46,613
  • 43
  • 151
  • 237
Anique
  • 61
  • 1
  • 8
  • C++ strings are different than C strings – qwr Apr 14 '21 at 05:14
  • 1
    `MessagetEnc.size()` is the length of a `std::string`. You cannot use `strlen` for that. You should avoid `using namespace std;` and learn to prefix standard types with `std::` or add a more narrow using statement. – Retired Ninja Apr 14 '21 at 05:14
  • 3
    You're using `std::string` to store strings, then you don't need to use `strlen()` to find out the length of it. Just use `MessagetEnc.size()` for the same purpose. – masoud Apr 14 '21 at 05:15

3 Answers3

7

There are two common ways to store strings in C++. The old C-style way, in this case you define an array of characters and \0 indicates the end of the string.

#include <cstring>

char str[500] = "Hello";
// How ever the capacity of str is 500, but the end of the actual string
// must be indicated by zero (\0) within the str and Compiler puts it
// automatically when you initialize it by a constant string.
// This array contains {'H', 'e', 'l', 'l', 'o', '\0'}

int len = std::strlen(str);
// To get the actual length you can use above function

Another way to define a string is to use std::string.

#include <string>

std::string str = "Hello";

int len = str.size();
          ~~~~~~~~~~
// or

int len = str.length();
          ~~~~~~~~~~~~

Footnote - You can carelessly use `std::strlen` on a `std::string` like this:
std::string str = "Be careful!";
int len = std::strlen(str.c_str());

// Note: The pointer obtained from c_str() may only be treated as a pointer
// to a null-terminated character string if the string object does not contain 
// other null characters.

Be careful, str.size() is not always equal to std::strlen(str.c_str())

masoud
  • 55,379
  • 16
  • 141
  • 208
3

strlen() is declared in <string.h>, and std::strlen() is declared in <cstring>. Either way, they take a const char* as input, not a std::string. You will have to use std::string::c_str() to pass in C++ strings, eg:

#include <iostream>
#include <string>
#include <cstring>

int main () 
{
    int EncCode; 
    std::cout << "Encryption code: " << std::endl;
    std::cin >> EncCode; 

    std::string MessagetEnc;
    std::cout << "Message to Encrypt:";
    std::cin >> MessagetEnc;
    std::cout << "Output: " << std::endl;

    int n = std::strlen(MessagetEnc.c_str());
    for (int i = 0; i < n; ++i)
    {
        std::cout << "Encrypted message" << MessagetEnc[i];
    }
}

However, there is no need to use (std::)strlen() with C++ strings at all. Use the std::string::size() method instead, eg:

#include <iostream>
#include <string>

int main () 
{
    int EncCode; 
    std::cout << "Encryption code: " << std::endl;
    std::cin >> EncCode; 

    std::string MessagetEnc;
    std::cout << "Message to Encrypt:";
    std::cin >> MessagetEnc;
    std::cout << "Output: " << std::endl;

    size_t n = MessagetEnc.size();
    for (size_t i = 0; i < n; ++i)
    {
        std::cout << "Encrypted message" << MessagetEnc[i];
    }
}

Which can be simplied further by using a range-based for loop instead, eg:

#include <iostream>
#include <string>

int main () 
{
    int EncCode; 
    std::cout << "Encryption code: " << std::endl;
    std::cin >> EncCode;

    std::string MessagetEnc;
    std::cout << "Message to Encrypt:";
    std::cin >> MessagetEnc;
    std::cout << "Output: " << std::endl;

    for (char ch : MessagetEnc)
    {
        std::cout << "Encrypted message" << ch;
    }
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
-1

Try to use cstring header file

In c++ strlen class is in cstring file.

Dharman
  • 30,962
  • 25
  • 85
  • 135