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

Checking string length, max and minimum

Is there a function to check if a string is too long or too short, I normally end up writing something like this in several places: if (strlen($input) < 12) { echo "Input is too short, minimum is 12 characters (20 max)."; } elseif(strlen($input)…
john mossel
  • 2,158
  • 5
  • 24
  • 39
3
votes
2 answers

How to use calloc() in C?

Shouldn't I get an error if my string goes over 9 characters long in this program? // CString.c // 2.22.11 #include #include #include main() { char *aString = calloc(10, sizeof(char)); if (aString == NULL) …
nambvarun
  • 1,201
  • 4
  • 13
  • 14
3
votes
3 answers

PHP String Length Without strlen()

Just browsing over the latest release of the PHP coding standards, and something caught my eye: http://svn.php.net/viewvc/php/php-src/trunk/CODING_STANDARDS?revision=296679&view=markup Coding standard #4 states that "When writing functions that deal…
Unpossible
  • 10,607
  • 22
  • 75
  • 113
3
votes
1 answer

string.h output words C

I need to compare the first and the last letter in a word; if these letters are the same, I need to output that word to a file. But I take words from another file. My problem is i can't guess how i should output all words because in my code, it…
Nikita Gusev
  • 143
  • 10
3
votes
6 answers

C: Custom strlen() library function

I created my version of strlen() function. unsigned int my_strlen(char *p) { unsigned int i = 0; while(*p!='\0') { i++; p++; } return i; } It gives me correct output everytime i run.But my collegues are saying…
Cody
  • 2,480
  • 5
  • 31
  • 62
3
votes
3 answers

How is the strlen calculated for a string without null character?

This code returns n=11, with 10th and 11th character as ' ' and '@' How does this work? How does strlen function take it as 11 characters? It seems like it takes the string length as 12 characters in some compilers. #include #include…
PyRookie
  • 75
  • 2
  • 4
3
votes
2 answers

strlen() not working with string variable

#include #include #include using namespace std; int main() { string b="hello"; cout<
Waters Sharma
  • 68
  • 1
  • 2
  • 11
3
votes
5 answers

Why is my Content-Length header wrong?

I am trying to find out the exact length of a string using strlen() in php 5.2. The string ($data) contains '\t' and '\n'. echo strlen($data); Code: // fetch table header $header = ''; while ($fieldData = $result->fetch_field()) { …
kobra
  • 4,853
  • 10
  • 32
  • 33
3
votes
8 answers

Is copying in a loop less efficient than memcpy()?

I started to study IT and I am discussing with a friend right now whether this code is inefficient or not. // const char *pName // char *m_pName = nullptr; for (int i = 0; i < strlen(pName); i++) m_pName[i] = pName[i]; He is claiming that for…
L. Resnik
  • 101
  • 1
  • 10
3
votes
3 answers

Segmentation fault with strlen when not ever using strlen?

I have some code which takes a file, reads each line into a new string array (and adds 128 to each character), then assigns each array into an array of pointers, then prints each array. When trying to run the code I get an error stating a…
Finlandia_C
  • 385
  • 6
  • 19
3
votes
3 answers

strlen returning wrong value

I'm building a text editor using doubly linked lists. These are my structs: #define N 4 typedef struct node { char data[N]; int size; struct node* next; struct node* prev; }node; typedef struct text { struct node* head; …
user3484582
  • 557
  • 1
  • 6
  • 22
3
votes
1 answer

warning: incompatible implicit declaration of built-in function 'strlen' and 'strcpy'

I just finnished my hangman game and as a last step I am doing some code cleanup and optimization, but I can't seem to understand why I receive the following two warnings: warning: incompatible implicit declaration of built-in function…
rampy
  • 33
  • 1
  • 1
  • 4
3
votes
3 answers

How to create a new char* in standard C

I have this code made for C++ (it works): char* ConcatCharToCharArray(char *Str, char Chr) { char *StrResult = new char[strlen(Str) + 2]; strcpy(StrResult, Str); StrResult[strlen(Str)] = Chr; StrResult[strlen(Str) + 1] = '\0'; …
soulblazer
  • 1,178
  • 7
  • 20
  • 30
3
votes
1 answer

php strlen shows different length of array elements

I am having a problem. I have a string in a variable. I split the string in to an array using explode function. But strlen function shows different lengths when I echo the string as an array element and echo the same string in strlen function. Below…
Riz
  • 185
  • 3
  • 7
  • 14
3
votes
3 answers

Counting special characters with PHP

I want to count the number of characters in a textfield on my website. The textfield accepts any type of input from a user, including ascii art and other special characters. If the user types in normal characters, I can use strlen($message) to…
user1399181
  • 303
  • 4
  • 10