🌐
GeeksforGeeks
geeksforgeeks.org › c language › strlen-function-in-c
strlen() function in c - GeeksforGeeks
May 29, 2023 - The strlen() function in C calculates the length of a given string. The strlen() function is defined in string.h header file.
🌐
TutorialsPoint
tutorialspoint.com › c_standard_library › c_function_strlen.htm
C library function - strlen()
The C library strlen() function is used to calculates the length of the string. This function doesn't count the null character '\0'. In this function, we pass the pointer to the first character of the string whose length we want to determine and as
🌐
Programiz
programiz.com › c-programming › library-function › string.h › strlen
C strlen() - C Standard Library
#include <stdio.h> #include <string.h> int main() { char a[20]="Program"; char b[20]={'P','r','o','g','r','a','m','\0'}; // using the %zu format specifier to print size_t printf("Length of string a = %zu \n",strlen(a)); printf("Length of string b = %zu \n",strlen(b)); return 0; } ... Note that ...
🌐
w3resource
w3resource.com › c-programming-exercises › c-snippets › implementing-custom-strlen-function-in-c.php
C - Implementing a custom strlen() function
Here's an implementation of a custom strlen() function: ... #include <stdio.h> size_t custom_strlen(const char* str) { size_t len = 0; while (*str != '\0') { len++; str++; } return len; } int main() { char str[] = "C Snippets"; size_t len = ...
🌐
W3Schools
w3schools.com › c › ref_string_strlen.php
C string strlen() Function
This is smaller than the amount of memory that is reserved for the string, which can be measured using the sizeof operator instead. The strlen() function is defined in the <string.h> header file.
🌐
Aticleworld
aticleworld.com › home › strlen function in c/c++ and implement own strlen()
strlen function in C/C++ and implement own strlen() - Aticleworld
April 18, 2022 - strlen function in C computes length of given string. It takes string as an argument and returns its length and doesn’t count null character.
🌐
Log2Base2
log2base2.com › c-examples › string › c-program-length-of-string-strlen-function.html
C program to find length of string using strlen function | strlen() c | Incompatible implicit declaration of built-in function strlen
#include<stdio.h> #include<string.h> ... strlen" #include&ltstring.h&gt · If we want to use strlen function in our source program, we should include string.h library....
🌐
Wikibooks
en.wikibooks.org › wiki › C_Programming › string.h › strlen
C Programming/string.h/strlen - Wikibooks, open books for an open world
#include <stdio.h> #include <string.h> int main() { char *string = "Hello World"; printf("%lu\n", (unsigned long)strlen(string)); return 0; } This program will print the value 11, which is the length of the string "Hello World". Character strings are stored in an array of a data type called char.
Find elsewhere
🌐
The Open Group
pubs.opengroup.org › onlinepubs › 9799919799 › functions › strlen.html
strlen
This volume of POSIX.1-2024 defers to the ISO C standard. The strlen() function shall compute the number of bytes in the string to which s points, not including the terminating NUL character.
🌐
Educative
educative.io › answers › how-to-use-the-strlen-function-in-c
How to use the strlen() function in C
Note: strlen() calculates the length of a string up to, but not including, the terminating null character. Return Value: The function returns the length of the string passed to it.
🌐
Cppreference
en.cppreference.com › w › c › string › byte › strlen.html
strlen, strnlen_s - cppreference.com
June 13, 2024 - They are pure utility functions used to provide limited support for non-null terminated strings. Run this code · #define __STDC_WANT_LIB_EXT1__ 1 #include <stdio.h> #include <string.h> int main(void) { const char str[] = "How many characters does this string contain?"; printf("without null character: %zu\n", strlen(str)); printf("with null character: %zu\n", sizeof str); #ifdef __STDC_LIB_EXT1__ printf("without null character: %zu\n", strnlen_s(str, sizeof str)); #endif } Possible output: without null character: 45 with null character: 46 without null character: 45 ·
🌐
OverIQ
overiq.com › c-programming-101 › the-strlen-function-in-c
The strlen() Function in C - C Programming Tutorial - OverIQ.com
This section discusses some commonly used functions provided by string library in C. These functions are declared in the header file string.h, so before using these function you must include string.h in your program. ... Note: For this chapter ignore the keyword const. It will be discussed later. The strlen() accepts an argument of type pointer to char or (char*), so you can either pass a string literal or an array of characters.
🌐
Scaler
scaler.com › home › topics › c strlen()
C strlen() - Scaler Topics
July 14, 2024 - The strlen() function in C returns the length of the string which is passed as the parameter to the function. The length of the string is returned in integer type excluding the NULL character. In the above code, We created a string named name.
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › strlen() in c
How to Use strlen() Function in C Effectively
October 10, 2024 - Get a comprehensive overview of the strlen() function in C. Learn its syntax, best practices, and how to avoid common errors in string length calculations.
🌐
Stack Overflow
stackoverflow.com › questions › 70116574 › creating-an-strlen-function-in-c
string - Creating an strlen function in c - Stack Overflow
Sign up to request clarification or add additional context in comments. ... I've just replaced your (3) and (4) with i++ that will increment until termination (\0) and will return (i). #include <stdio.h> #define MAXLENGTH 80 int my_strlen(char* s) { int i=1; char *p = s; while (*p++) i++; return (i); } int main() { char str[MAXLENGTH]; int len; printf("Enter a string:"); gets(str); len = my_strlen(str); printf("The length of the string %s is %d\n", str, len); }
Top answer
1 of 6
6

There's a good chance that you're doing this to a string that you have obtained with fgets or a similar input function. In that case, it may well have the newline at the end still.

If you change your code temporarily to:

void xyz (char *number) {
    int i = 0, length = strlen (number);
    while (i < length)
        printf ("Number[%d]: %c (%d)", i, number[i], number[i]);
        i++;
    }
}

that should also show the numeric codes for all characters.

The problem with encoding something like that - 2 in your function is that it will not work with:

xyz ("123");

since it will stop early, printing out only 12. The caller should be calling with valid data, meaning that it should adjust the value to be a numeric string before calling.


You can see this happening in the following program:

#include <stdio.h>
#include <string.h>

void xyz (char *number) {
    int i = 0, length = strlen(number) - 2;
    while(i <= length)
    {
        printf("Number[%d]: %c (%d)\n",i, number[i], number[i]);
        i++;
    }
    puts ("===");
}

void xyz2 (char *number) {
    int i = 0, length = strlen(number);
    while(i < length)
    {
        printf("Number[%d]: %c (%d)\n",i, number[i], number[i]);
        i++;
    }
    puts ("===");
}

int main (void) {
    char buff[100];
    printf ("Enter number: ");
    fgets (buff, sizeof (buff), stdin);
    xyz (buff);
    xyz ("12345");
    xyz2 (buff);
    xyz2 ("12345");
    return 0;
}

The (annoted) output of this, if you enter 98765, is:

Enter number: 98765
Number[0]: 9 (57)
Number[1]: 8 (56)
Number[2]: 7 (55)  # Your adjustment works here because of the newline.
Number[3]: 6 (54)
Number[4]: 5 (53)
===
Number[0]: 1 (49)
Number[1]: 2 (50)
Number[2]: 3 (51)  # But not here, since it skips last character.
Number[3]: 4 (52)
===
Number[0]: 9 (57)
Number[1]: 8 (56)
Number[2]: 7 (55)  # Here you can see the newline (code 10).
Number[3]: 6 (54)
Number[4]: 5 (53)
Number[5]:
 (10)
===
Number[0]: 1 (49)
Number[1]: 2 (50)
Number[2]: 3 (51)  # And proper numeric strings work okay.
Number[3]: 4 (52)
Number[4]: 5 (53)
===

If you're looking for a robust user input function that gets around this problem (and avoids dangerous things like unbounded scanf("%s") and gets), I have one elsewhere on SO (right HERE, in fact) drawn from my arsenal.

2 of 6
1

Check if this works for you --

void xyz(char *number)
{
    int length = strlen(number);

    while(i < length)
    {
        printf("Number[]: %c",number[i]);
        i++;
    }
}

and this function, if invoked as

xyz("1234");

should print out:

Number[]: 1
Number[]: 2
Number[]: 3
Number[]: 4

Is that what you really wanted ? If so, then let me point 2 mistakes.

1) "i" is not initialized. It is more a question of good practise. Explicitly initialize your loop control variable (to zero in this case), just don't assume it to be set. 2) your while loop condition with "<=" runs 1 extra cycle that it should.

Remember that arrays start from index '0' (zero), and an array of size 10, has valid index from 0 to 9, and C lang uses null character ('\0'), to terminate a string. So, your "1234" is actually stored as:-

string[0] = '1' string[1] = '2' string[2] = '3' string[3] = '4' string[4] = '\0' (<= NULL)

so if your loop-counter (control variable) i=0 at beginning of loop, for first iteration, you pick string[0], and for 2nd iteration (when i=1) you pick string[1]... and this way, the loop should run only 4 times, i.e. when i==4 (i.e. loopcounter < string-length), you must stop & exit loop.

Hope this clears up your doubt and help. If so, please don't forget to accept the answer.

🌐
Quora
quora.com › How-does-strlen-work-in-C
How does strlen work in C? - Quora
Answer (1 of 2): Below is the code definition for strlen function in C: long strlen(char *s) { long length = 0; for(; s[length] != '\0'; ++ length); return length; } The function increments the length by 1 for very character that is not NULL.