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
4
votes
4 answers

Why is my (re)implementation of strlen wrong?

I came up with this little code but all the professionals said its dangerous and I should not write code like this. Can anyone highlight its vulnerabilities in 'more' details? int strlen(char *s){ return (*s) ? 1 + strlen(s + 1) : 0; }
Bálint Juhász
  • 304
  • 1
  • 16
4
votes
3 answers

How could a PHP value contain a string of zero length not equal to the empty string or null?

I am retrieving some data from an in-house store and in case of failure, I get a very specific response. Calling strlen() on this variable returns the value of zero. It is also not equal to NULL or "". I'm using this code to test: if ($data ===…
Erick Robertson
  • 32,125
  • 13
  • 69
  • 98
4
votes
2 answers

For any string "char name[10]="test"",is strlen(name)+1 always guaranteed to be same as sizeof(name)?

For a string name[],can we use strlen(name)+1 and sizeof(name) interchangeably in our code without second thought?Aren't they same?I checked about it and found out even the return type for both is same,size_t.And after all sizeof calculates the size…
Thokchom
  • 1,602
  • 3
  • 17
  • 32
4
votes
2 answers

Why is absence of array index in "extern char name[]" not affecting strlen(name) but causing error for sizeof(name)?

Now,from what I understand,the extern in the declaration of name[] tells the compiler that its definition is somewhere else (In my program,I have defined it below the part where I have used it).But why then there is different consequence for…
Thokchom
  • 1,602
  • 3
  • 17
  • 32
4
votes
4 answers

Where C++ really stores a string if the char array that stores it is smaller than a string is?

I'm testing an example about strings in C++ from "C++ Premiere" book. const int size = 9; char name1[size]; char name2[size] = "C++owboy"; // 8 characters here cout << "Howdy! I'm " << name2 << "! What's your name?" << endl; cin >> name1; // I…
Green
  • 28,742
  • 61
  • 158
  • 247
3
votes
4 answers

What is the size of a double pointer string?

Assume I have char **argv. How do I determine its size? I have a string - an example would be: sleep 30 & that is held in argv. I would like to be able to access the last array in *argv. In this case, the last array contains &. How can I access it?…
darksky
  • 20,411
  • 61
  • 165
  • 254
3
votes
3 answers

How to strlen of a multi-language string

I want to get strlen() of Shift-jis and Utf-8, then compare them. A string could be mixed "ああ12345678sdfdszzz". I tried to use strlen but it generates the different results. mb_strlen also doesn't help because this is a mixed string. For…
emeraldhieu
  • 9,380
  • 19
  • 81
  • 139
3
votes
3 answers

PHP - strlen not working

In Register.php, it contains a form.
Email:
Username:
Lloydworth
  • 743
  • 6
  • 20
  • 38
3
votes
1 answer

static size_t strnlen(const char *s, size_t max) -- why a static return value?

I might be going insane, but I don't think I've ever seen this in c++ (though my reference code is in C). Why is there a static on the return value of the code here and what impact does it have? I don't think I've ever seen a static function…
John Humphreys
  • 37,047
  • 37
  • 155
  • 255
3
votes
3 answers

Why is the param of strlen a "const"?

I'm learning the C language. My question is: Why is the param of strlen a "const" ? size_t strlen(const char * string); I'm thinking it's because string is an address so it doesn't change after initialization. If this is right, does that mean every…
conradkleinespel
  • 6,560
  • 10
  • 51
  • 87
3
votes
7 answers

substr with strlen

I have an amount like 0003000, the last 2 digits are the decimal number. I want to transform 0003000 to 00030,00 (insert a decimal comma in front of the last 2 digits). I tried to do this with substring. I take the length of the string with strlen…
Markey
  • 33
  • 1
  • 3
3
votes
2 answers

Why are variables of type size_t casted to (unsigned) in a printf call?

I was writing a program and looking up strlen() function on this website http://www.cplusplus.com/reference/cstring/strlen/. I saw that in the example where the function was used, the author of the code put (unsigned) to cast the result of the…
Damian Kowalski
  • 362
  • 2
  • 8
3
votes
1 answer

How to traverse a string in assembly until I reach null? (strlen loop)

Right now I'm just figuring out how to even traverse a string. If the code doesn't make sense, it's because I've interpreted some information wrong. At the worst, I don't really know what I'm doing. strlen: pushq %rbx movq %rsi, %rbx loop: …
3
votes
7 answers

Different ways to calculate string length

A comment on one of my answers has left me a little puzzled. When trying to compute how much memory is needed to concat two strings to a new block of memory, it was said that using snprintf was preferred over strlen, as shown below: size_t length =…
user142162
3
votes
3 answers

How to strlen first two parts of an array?

I have an array like, $words = array ('This', 'is', 'a', '', '', '', 'string'); and I was looking for a function that counts characters of the first two parts, in this case, this and is should be counted . the result for these two will be 5. I…
Hadi Ahmadi
  • 129
  • 12