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

Bus error while running a simple string C program

I was running this simple program, the output i get is a "bus error". using some debugging statements i found the point at which it occurs was at the strcat() call. #include #include main() { char *s = "this is "; char *s1 = "me";…
Maverickgugu
  • 767
  • 4
  • 13
  • 26
4
votes
1 answer

Get C string length of a 16 or 32-byte fixed-size buffer? (XMM or YMM register width)

Is there any way to get the length of an ASCII string that is stored in a 16- or 32-byte buffer by loading it into an XMM or YMM register? Essentially I am looking for the index (in bits or bytes) of the first zero byte. My goal is to avoid looping…
user11472602
4
votes
4 answers

Function to check if string length in greater than or less than required amount

I want to create a function to check if the length of a string is greater than or less than a required amount: Something like this: function check_string_lenght($string, $min, $max) { if ($string == "") { return x; } elseif…
johnathan ross
  • 43
  • 1
  • 1
  • 3
4
votes
8 answers

PHP Insert Multiple Spaces

I've got some data that needs to be cleaned up into a fixed length format. I'm using PHP to grab the data out, covert it, and put it back in, but it's not working as planned. There is a certain point in each piece in the middle of the data where…
bmbaeb
  • 520
  • 1
  • 8
  • 15
4
votes
2 answers

What is strlen elision?

I can see it listed as one of the modern C++ idioms, but what it is exactly? Is it just a type of copy elision?
user5560811
4
votes
7 answers

Usage of fgets function in C

One of my assignments in to write my own UNIX Shell. To receive input from the user, I am using fgets to capture the input as a string but I'm not really sure how it works. When I run: char command[50]; fgets(command, sizeof(command),…
user446836
  • 733
  • 4
  • 16
  • 24
4
votes
2 answers

How does strlen works with numbers?

I am trying to figure out how strlen() finds the length of a number. When I try the following: echo strlen(00000000000000000000000000000000); it outputs 1 but when I try echo strlen(11111111111111111111111111111111111111111111111111111111); it…
Qeaxe
  • 81
  • 8
4
votes
3 answers

C char pointer length

This was a quiz (not graded) on Coursera. The question was, what does the following code possibly evaluate to? The correct answers were 127 and 0 (other options were crash, -1, 128. Why does the following code possibly evaluate to 0? I understand…
Nathan Fowler
  • 561
  • 5
  • 19
4
votes
4 answers

string length with fgets function in C

I have a problem. I've tried to see the length of some string after using fgets function. If I enter string under the number of letter which can be in the string (like: the maximum letters in string is 9 and I enter 4 letters), I get length of the…
Raz Omry
  • 255
  • 1
  • 6
  • 18
4
votes
1 answer

Understanding the magic number 0x07EFEFEFF used for strlen optimization

I stumbled upon this answer regarding the utilization of the magic number 0x07EFEFEFF used for strlen's optimization, and here is what the top answer says: Look at the magic bits. Bits number 16, 24 and 31 are 1. 8th bit is 0. 8th bit represents…
aldeb
  • 6,588
  • 5
  • 25
  • 48
4
votes
4 answers

Is a strlen call in snprintf causing this segfault?

I have a void *, call it data, whose length I know, but is not null terminated. I make a call like this snprintf(line, sizeof(line), "%*s", n, (const char*)data) where n is the known length. Almost always this works, but occasionally it results in…
pythonic metaphor
  • 10,296
  • 18
  • 68
  • 110
4
votes
1 answer

How to determine if a byte is null in a word

I am reading the "strlen" source code from the glibc, and the trick developers found to speed it up is to read n bytes where n is the size of a long word, instead of reading 1 byte at each iteration. I will assume that a long word has 4 bytes. The…
Brendan Rius
  • 610
  • 9
  • 18
4
votes
4 answers

getting the length of an array using strlen in g++ compiler

could someone explain why i am getting this error when i am compiling the source using following g++ compiler #include #include using namespace std; int main() { char source_language[50]; …
KItis
  • 5,476
  • 19
  • 64
  • 112
4
votes
3 answers

C Programming - Functionality of strlen

I'm working to try and understand some string functions so I can more effectively use them in later coding projects, so I set up the simple program below: #include #include int main (void) { // Declare variables: char…
Ryan Barker
  • 113
  • 2
  • 11
4
votes
1 answer

string length for mips assembly

Whenever I run the following code: #counts length of a string .data .data string: .asciiz "Hello" printedMessage: .asciiz "The length of the string: " .text main: la $a0, string # Load address of string. jal strlen …
MCsuchNsuch
  • 256
  • 2
  • 5
  • 17