How do I use strlen on a C string

0 votes

If that makes sense, I'm attempting to convert my basic C code to C++ using C++ syntax rather than C syntax. 

But I have an issue. 

In C++, I'm not sure how to use strlen(). 

I have #include iostream> #include string> and namespace std; in preprocessing. 

When I attempted to compile, I received 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, #include <cstring> does not appear to solve the problem.

The code is as follows:

#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 for beginners, but I wanted to give it a shot after reading a few articles, as I intend to study it thoroughly after I'm out of the "beginning stage."

Edit: std:: is there because I attempted to avoid using namespace std; as a debugging tool.

Aug 1, 2022 in C++ by Nicholas
• 7,760 points
1,184 views

1 answer to this question.

0 votes

In C++, there are two standard methods for storing strings. 

In this scenario, you specify an array of characters, and 0 signifies 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();
answered Aug 2, 2022 by Damon
• 4,960 points

Related Questions In C++

0 votes
0 answers

How do I declare a 2d array in C++ using new?

How do I declare a two-dimensional array using new? For example, for a "typical" array, I would:      int* ary = new int[Size] but int** ...READ MORE

Jul 15, 2022 in C++ by Nicholas
• 7,760 points
481 views
0 votes
1 answer

How do i apply lower_bound to a range of unsorted vector elements?

What's the point of sorting your array? ...READ MORE

answered Jun 15, 2022 in C++ by Damon
• 4,960 points
1,754 views
0 votes
1 answer

how could I use the power function in c/c++ without pow(), functions, or recursion

It is part of a series.  Replace pow() with the previous iteration's value. There is no need for code to call pow ().  Pow(x, 5 * I - 1) and pow(-1, I - 1) may be formed since both have an int exponent dependent on the iterator I from the previous loop iteration. Example: Let f(x, i) = pow(x, 5 * i ...READ MORE

answered Jun 21, 2022 in C++ by Damon
• 4,960 points
2,506 views
0 votes
0 answers

When and why do I need to use cin.ignore() in C++?

In C++, I developed a simple application that requested the user to enter a number and then a string.  Surprisingly, when I ran the application, it never paused to ask for the string.  It simply ignored it.  After conducting some research on StackOverflow, I discovered that I needed to include the following line: cin.ignore(256, '\n'); before the line with the string input  That addressed the problem and allowed the software to run.  My issue is why C++ need the cin.ignore() line, and how can I forecast when I will need to use it. Here's the software I created: #include <iostream> #include <string> using namespace std; int main() { ...READ MORE

Jul 4, 2022 in C++ by Nicholas
• 7,760 points
750 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 11, 2018 in Python by Priyaj
• 58,020 points
677 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 21, 2018 in Python by Priyaj
• 58,020 points
2,372 views
0 votes
1 answer

How to pass large records to map/reduce tasks?

Hadoop is not designed for records about ...READ MORE

answered Sep 25, 2018 in Big Data Hadoop by Frankie
• 9,830 points
1,438 views
0 votes
1 answer

Invalid method parameters for eth_sendTransaction

params needs to be an array, try {"jsonrpc":"2.0","method":"eth_se ...READ MORE

answered Sep 28, 2018 in Blockchain by digger
• 26,740 points
1,844 views
0 votes
1 answer

How do I reverse a C++ vector?

The algorithm header has a method std::reverse for this purpose. #include <vector> #include <algorithm> int main() { std::vector<int> ...READ MORE

answered Jun 27, 2022 in C++ by Damon
• 4,960 points
735 views
0 votes
1 answer

How can I convert a std::string to int?

There are some new convert methods in C++ that convert std::string to a numeric type. As an alternative to str.c str() atoi(str.c str()) atoi(str.c str() you can make use of std::stoi std::stoi ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
688 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP