2
I have this code:
#include <iostream>
#include <string.h>
using namespace std;
int main() {
int len = strlen("Teste");
cout << len << endl;
return 0;
}
That generates the output:
5
In the code above, I was using the function strlen() in C++ to count the number of characters in the string "Teste". So far so good, but when I create a variable like string:
#include <iostream>
#include <string.h>
using namespace std;
int main() {
string nome = "Teste";
int len = strlen(nome);
cout << len << endl;
return 0;
}
The program gives error. Why? The function strlen() does not accept variables? So I went there and tried otherwise, I changed the type of variable to type char, then the code went like this:
#include <iostream>
#include <string.h>
using namespace std;
int main() {
char nome[10] = "Teste";
int len = strlen(nome);
cout << len << endl;
return 0;
}
And this time it worked, he returned the number of characters of the word, but I ask, char nome[10] = "Teste"; is not the same thing as string nome = "Teste";? Why didn’t it work with the type variable string?
Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site.
– Maniero