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
22
votes
3 answers

Best way to get length of const char * in c++

i know two way's to get length of const char * const char * str = "Hello World !"; int Size = 0; while (str[Size] != '\0') Size++; and other way is very simple const char * str = "Hello World !"; size_t Size = strlen(str); but i don't want to use…
JamesAlb
  • 263
  • 1
  • 3
  • 7
21
votes
8 answers

How to find the length of argv[] in C

#include #include #include int main(int argc, char *argv[]){ int fir; //badly named loop variable char *input[] = calloc( strlen(argv), sizeof(char)); //initializing an array for( fir = 1; fir<…
user1787351
  • 223
  • 1
  • 2
  • 5
20
votes
5 answers

which C++ header file declares strlen?

Which library does strlen() belong to? Does it belong to cstring? or string? I tried the following code, and it does work: include using namespace std; //withou include int main() { char * str="abc"; …
user2756494
  • 263
  • 1
  • 4
  • 7
19
votes
2 answers

Why reimplement strlen as loop+subtraction?

Inspired by this question about the following code from SQLite3: static int strlen30(const char *z){ const char *z2 = z; while( *z2 ){ z2++; } return 0x3fffffff & (int)(z2 - z); } that is accompanied by a commit message saying this…
sharptooth
  • 167,383
  • 100
  • 513
  • 979
18
votes
1 answer

How does the strlen function work internally?

How does strlen() work internally? Are there any inherent bugs in the function?
Manu
  • 5,534
  • 6
  • 32
  • 42
18
votes
6 answers

Check if php get variable is set to anything?

I need to check if variables are set to something. Up till now I have been using strlen(), but that is really embarrassing as I am pretty sure that is not a very efficient function to be using over an over again. How do I perform this sort of check…
Amy Neville
  • 10,067
  • 13
  • 58
  • 94
16
votes
4 answers

can I count on my compiler to optimize strlen on const char *?

In my SAX xml parsing callback (XCode 4, LLVM), I am doing a lot of calls to this type of code: static const char* kFoo = "Bar"; void SaxCallBack(char* sax_string,.....) { if ( strcmp(sax_string, kFoo, strlen(kFoo) ) == 0) { } …
Jacko
  • 12,665
  • 18
  • 75
  • 126
14
votes
6 answers

strlen() and UTF-8 encoding

Assuming UTF-8 encoding, and strlen() in PHP, is it possible that this string has a length of 4? I'm only interested to know about strlen(), not other functions This is the string: $1�2 I have tested it on my own computer, and I have verified…
Jon Lyles
  • 345
  • 1
  • 3
  • 12
12
votes
4 answers

Why does this function return the correct length of a string? (Incrementing a char pointer)

This is a function that counts the number of characters in a string: int str_len(const char* s) { int i = 0; while(*(s++)) { i++; } return i; } Why does this return the correct length? Let's say I call this function with a…
lor
  • 123
  • 5
12
votes
3 answers

C++11 char16_t strlen-equivalent function

I have a simple question: Is there a way to do a strlen()-like count of characters in zero-terminated char16_t array?
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
12
votes
9 answers

C strlen() implementation in one line of code

Yesterday I was at interview and was asked to implement strlen() in C without using any standard functions, all by hands. As an absolute amateur, I implemented primitive version with while loop. Looking at this my interviewer said that it can be…
Olex
  • 1,656
  • 3
  • 20
  • 38
9
votes
5 answers

How can I use strlen in php for Persian?

I have this code: $string = 'علی'; echo strlen($string); Since $string has 3 Persian characters, output must be 3 but I get 6. علی has 3 characters. Why my output is 6 ? How can I use strlen() in php for Persian with real output?
user3932710
  • 125
  • 5
9
votes
3 answers

strlen performance implementation

This is a multipurpose question: How does this compare to the glibc strlen implementation? Is there a better way to to this in general and for autovectorization. #include #include #include #include…
nwmcsween
  • 361
  • 2
  • 12
8
votes
9 answers

Is There A Difference Between strlen()==0 and empty()?

I was looking at some form validation code someone else had written and I saw this: strlen() == 0 When testing to see if a form variable is empty I use the empty() function. Is one way better than the other? Are they functionally equivalent?
red4d
  • 277
  • 2
  • 4
  • 10
8
votes
5 answers

How to strcpy and return number of copied characters?

I want to copy a null-terminated string to another location and want to know how long the copied string was. Efficiency is of utmost importance. There ist the strcpy function which can achieve this, but it does not return how many characters are…
gexicide
  • 38,535
  • 21
  • 92
  • 152
1
2
3
46 47