Questions tagged [strlen]

A standard C function that returns the length of a string.

This function calculates the length of the string, excluding the terminating null byte \0. It is defined in the <string.h> standard header.

Documentation for C strlen.

692 questions
6
votes
1 answer

What is the efficiency of strlen() in PHP?

Does using strlen actually count the number of bytes in the string by iterating through the string, or does it simple return the value of an already calculated length of the string from an index? The reason for my question is because I have a choice…
Alasdair
  • 13,348
  • 18
  • 82
  • 138
5
votes
2 answers

Valgrind errors on simple C string functions

Let's consider this simple test program: #include #include int main(int argc, char *argv[]) { char buf[256]; int i; strcpy(buf,"Hello world!"); i = strlen(buf); printf("Length of string…
Martin B.
  • 53
  • 1
  • 3
5
votes
6 answers

parallel strlen?

I'm wondering if there would be any merit in trying to code a strlen function to find the \0 sequence in parallel. If so, what should such a function take into account? Thanks.
Dervin Thunk
  • 19,515
  • 28
  • 127
  • 217
5
votes
0 answers

Valgrind error with new array

I have the following code: int main(int argc, char** argv) { char* p = new char[11]; strcpy(p, "1234567890"); cout << strlen(p) << endl; delete[] p; return 0; } It allocates 11 bytes and then copies a string of 10 bytes plus a…
imreal
  • 10,178
  • 2
  • 32
  • 48
5
votes
3 answers

why is -1>strlen(t) true in C?

Working on this little piece of code in VS2013, but for some reason it doesn't print.it seems that -1>strlen(str) Anyone got an idea what i'm doing wrong char *str="abcd"; if(-1
Tj L
  • 69
  • 5
5
votes
4 answers

How to determine array length of uint8_t?

I'm trying to determine array length of msg on the below code. I used strlen and sizeof but they don't return 6. What function can I use to determine the length of uint8_t array or how can I modify the below code (osal_DataLenght() func)? int…
sven
  • 1,101
  • 7
  • 21
  • 44
5
votes
3 answers

passing argument 1 of 'strlen' differ in signedness

I use strlen() call all over my project, until now I compiled my project without -Wall compiler option. But when I start using -Wall I face so many compiler warning. 80% are the strlen char * vs const char * warning. I am aware of type casting all…
sakthi
  • 49
  • 1
  • 1
  • 3
5
votes
4 answers

Size of string literal consisting of escaped characters

Code in question: #include #include using namespace std; int main() { cout << sizeof("\n\r\t") << endl; // prints 4 cout << strlen("\n\r\t") << endl; // print 3 return 0; } I am confused because I always thought…
smac89
  • 39,374
  • 15
  • 132
  • 179
5
votes
2 answers

How the magic bits are improving the strlen function in glibc

I was going through the source of strlen for glibc. They have used magic bits to find the length of string. Can someone please explain how it is working. Thank you
Aakashdeep
  • 123
  • 1
  • 13
4
votes
3 answers

strlen function giving wrong value

hello every one I have written code char sentence[100]; scanf("%s" ,sentence); char * ptrs = sentence ; printf("%d", strlen(ptrs)); suppose I enter john is a boy the strlen() function is giving me value 4 it stops counting after space what I…
mainajaved
  • 7,905
  • 10
  • 36
  • 44
4
votes
1 answer

How to input an empty string in c from command prompt

I have been revising my skills in the way I came to C language first to start from scratch I am working out few problems myself. In the way I am writing a program which outputs the length of the entered string the code goes like…
4
votes
3 answers

How do I use 'strlen()' on a C++ string

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 #include and using…
Anique
  • 61
  • 1
  • 8
4
votes
1 answer

Where is the source code for strlen() function in PHP?

I was looking through php-src/Zend/zend_API.c and couldn't find the source code for the strlen() function in PHP anywhere. Grepping through the code base didn't really help as it's littered with libc strlen everywhere. Googling doesn't much help…
emptyheap
  • 83
  • 6
4
votes
2 answers

How does std::strlen wоrk internally?

I've been struggling to understand std::strlen() but in vain: AFAIK strlen() returns the number of characters in a null-terminated constant character string in terms of bytes. If it is not null-terminated then the behavior is undefined. Apart from…
Maestro
  • 2,512
  • 9
  • 24
4
votes
7 answers

Why strlen function works without #include?

Quick question: strlen[char*] works perfectly regardless whether I #include or not All I get from compiler is a warning about implicit declaration, but functionally it works as intended. Why is that?
James Raitsev
  • 92,517
  • 154
  • 335
  • 470