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
2
votes
5 answers

What is efficient way to test zero length string: `strlen(str) > 0` or `*str`

Assuming char *str;, to test the empty string following approaches works /* Approrach 1 */ if (str && strlen(str) > 0) { ... } /* Approach 2 */ if (str && *str) { ... } What is the most preferable to use? I assume second will be faster as…
Shubham
  • 628
  • 1
  • 9
  • 19
2
votes
4 answers

Scanf reads only one word from sentence

I'm new at C and I have a little problem. I need to write a program where the user enters text and the program shows how long it is. I tried with strlen(), but it's not what I want. char a[20]; scanf("%s",a) And if I type "Hello world" the result…
NewAtC
  • 55
  • 9
2
votes
3 answers

if statement selects the wrong condition

I have a problem with this code: I don't know why it prints B and not A, since the condition is true. strlen(x) is clearly greater than i. Can you help me? #include #include int main() { char x[]="Hello"; int i = -3; …
Placido
  • 23
  • 2
2
votes
3 answers

Variable string limit "%*s" with sscanf() ==> "data argument not used by format string"

int y = strlen(target); sscanf(p,"%*s",y,buffer); Why does the above code result in warning: data argument not used by format string ?? Compiled with Apple clang version 11.0.0 (clang-1100.0.33.17). Aim : To get the same number of characters as in…
user13863346
  • 327
  • 2
  • 11
2
votes
2 answers

why strlen() function is giving wrong value

I don't understand why strlen() is giving wrong value (ie: 9) only for the first string. After that the values are coming correctly. #include #include int main() { int m, i, j; i = 2; j = 5; char a[i][j]; …
Pranshu_Taneja
  • 186
  • 1
  • 1
  • 16
2
votes
1 answer

Strlen Valgrind invalid read of size 1

In this program, I am trying to take the information stored in a file and then search through it to find the number stored. I removed the part where I iterate through the file to find the number that is stored because that part works fine. However,…
2
votes
1 answer

Why the function strlen() returns different values for the same length of two char arrays?

#include #include int main() { char a[50],b[50];// same sized arrays for(int j =0;j<50;j++){ b[j]='b';a[j]='a';// initializing with the same number of elements } printf("the size of a is…
2
votes
1 answer

My program for checking the contents and length of the string is having some issues

I am not able to type the second input in the command prompt, it says my program has stopped. I am just trying my own code. I have added two functions to check for the length and contents of the string. And i can simply use strcmp(), but i am…
Sridhar
  • 21
  • 3
2
votes
3 answers

Cannot get the length of arr[] : Implicit conversion loses integer precision: 'unsigned long' to 'int' for strlen(arr)

Implicit conversion loses integer precision: 'unsigned long' to 'int' I want to get the length of words[] but it shows me this. How can I fix it? int main(int argc, const char * argv[]) { char test[] = "ls test"; f_sparce_arg(test); …
Chener Zhang
  • 129
  • 9
2
votes
1 answer

Can someone explain to me how to walk through this code?

I want to understand what this code is supposed to do, 'cause Code::Blocks doesn't show me enough to understand on my own. The code in C language is down bellow: #include #include #include struct S { char…
2
votes
6 answers

strlen to return size_t?

In C: My string length function is returning a size_t value? Why is it not returning a integer which is conventional? And one more thing I noticed was that when I was trying concatenate this string with another string I received a bus error when I…
Maverickgugu
  • 767
  • 4
  • 13
  • 26
2
votes
1 answer

How I can extract ordered questions and answers from a block of plain text

Questions are also available for plain text. I want to be able to use PHP, Regex. However, because my Regex information is not enough, I tried to do this with the PHP function. Example plain text: (starting 12th question -> 15. question, here total…
Editor
  • 622
  • 1
  • 11
  • 24
2
votes
1 answer

PHP - Why some string function name without underscore e.g strlen?

I've notice some PHP string function are used with underscore and some are not Here are examples strlen() strtolower() strtoupper() strpos() etc.. String function with underscore str_replace() str_word_count() str_split() str_repeat() etc.. I…
Jaydeep Chauhan
  • 122
  • 2
  • 9
2
votes
1 answer

why c++ std::string.length() is slower than strlen() on VS2017?

I have tested c++ std::string.length() vs strlen() function on VS2017 as below. The result is: what surprised me is that string.length() is 7x slower than strnlen(). But I suppose string.length() is an O(1) operation, and strlen() is an O(n)…
ricky
  • 2,058
  • 4
  • 23
  • 49
2
votes
1 answer

Use strlen with scanf(%ms)

Is it possible to use strlen() over a dynamically allocated string? FOR EXAMPLE: #include #include int main () { char *input=NULL; printf ("Enter a sentence: "); scanf("%ms", &input); //Is this legit? printf ("The…
Mattia Surricchio
  • 1,362
  • 2
  • 21
  • 49