Length of a String Without Using strlen() Function in C Last Updated : 05 Dec, 2024 Comments Improve Suggest changes Like Article Like Report The length of a string is the number of characters in it without including the null character. C language provides the strlen() function to find the lenght of the string but in this article, we will learn how to find length of a string without using the strlen() function.The most straightforward method to find length of a string without using the strlen() function is by using a loop to traverse the whole string while counting the number of characters. Let’s take a look at an example: C #include <stdio.h> int findLen(char* s) { int l = 0; // Count each character from start to end while (s[l]) l++; return l; } int main() { char s[] = "Geeks"; printf("%d", findLen(s)); return 0; } Output5There are also a few other methods in C to find length of a string without using the strlen() function. Some of them are as follows:Table of ContentUsing RecursionUsing Pointer Arithmetic TrickUsing Pointer SubtractionUsing RecursionThe number of characters in the string can also be counted using recursion. C++ #include <stdio.h> int findLen(char* s) { if (!*s) return 0; // Add the current character to the count // and find length of rest of the string return 1 + findLen(s + 1); } int main() { char s[] = "Geeks"; // Find length of string s printf("%d", findLen(s)); return 0; } Output5Using Pointer Arithmetic TrickIncrement the pointer to the string array (different from the pointer to the first element of the string), dereference it and subtract the pointer to the first character of the string. C #include <stdio.h> int main() { char s[] = "Geeks"; // Calculate the length of the string using // pointer arithmetic int l = *(&s + 1) - s - 1; printf("%d", l); return 0; } Output5Note: This method can only be used when the string is declared as character array and only inside the same scope as string is declared.Using Pointer SubtractionUse pointer subtraction to find the difference between the pointer to the first and last character of the string, calculating the length excluding the null terminator. C #include <stdio.h> int findLen(char* s) { char* last = s; // Go to the last character while(*last++); // Return difference between last and s return last - s - 1; } int main() { char s[] = "Geeks"; // Print the length of string s printf("%d", findLen(s)); return 0; } Output5 Comment More infoAdvertise with us Next Article Length of a String Without Using strlen() Function in C A anushamahajan5 Follow Improve Article Tags : C Programs C Language C-Loops & Control Statements C-String C Examples +1 More Similar Reads How to Find Length of a String Without string.h and Loop in C? C language provides the library for common string operations including string length. Otherwise, a loop can be used to count string length. But is there other ways to find the string length in C apart from above two methods. In this article, we will learn how to find length of a string without strin 2 min read C Program to Find the Length of a String The length of a string is the number of characters in it without including the null character (â\0â). In this article, we will learn how to find the length of a string in C.The easiest way to find the string length is by using strlen() function from the C strings library. Let's take a look at an exa 2 min read Lex program to find the Length of a String Problem: Write a Lex program to find the Length of a String Explanation: FLEX (Fast Lexical Analyzer Generator) is a computer program that generates lexical analyzers and was written by Mike Lesk and Eric Schmidt. Lex reads an input stream specifying the lexical analyzer and outputs source code impl 1 min read C Program to Concatenate Two Strings Without Using strcat String concatenation is the process of appending one string to the end of another. C language provides strcat() library function to concatenate string but in this article, we will learn how to concatenate two strings without using strcat() in C.The most straightforward method to concatenate two stri 3 min read Converting String to Long in C Here, we will see how to build a C Program For String to Long Conversion using strtol() function. Syntax: long int strtol(char *string, char **ptr, int base)The first argument is given as a stringThe second argument is a reference to an object of type char*The third argument denotes the base in whic 4 min read C Program to Print the Length of a String using Pointers C language provides a built-in function strlen() but in this article, we will learn how to find and print the length of the string using pointers in C.The easiest method to find the length of a string using pointers is by calculating difference between the pointers to the first and last character of 2 min read C Program to Print the Length of a String Using %n Format Specifier In C, strings are arrays of characters terminated by a null character ('\0') and the length of a string is the number of characters before this null character. In this article, we will learn how to find the length of the string using %n format specifier.The %n is a special format specifier for print 1 min read Copy N Characters from One String to Another Without strncat() In C, strings are the sequence of characters that are used to represent the textual data. Two strings can be easily concatenated or joined using the strncat() function of the C standard library up to n characters. In this article, we will learn how to concatenate the first n characters from one stri 2 min read C Program to Concatenate Two Strings Using a Pointer Concatenating two strings means appending one string at the end of another string. While the standard library provides strcat() for concatenation, this article will demonstrate how to concatenate two strings using pointers.To concatenate two strings using pointers, traverse the first string to its n 1 min read Concatenating Two Strings in C Concatenating two strings means appending one string at the end of another string. In this article, we will learn how to concatenate two strings in C.The most straightforward method to concatenate two strings is by using strcat() function. Let's take a look at an example:C#include <stdio.h> #i 2 min read Like