How to Find Length of a String Without string.h and Loop in C? Last Updated : 05 Dec, 2024 Comments Improve Suggest changes Like Article Like Report 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 string.h and loop in C.The most straightforward method to find length of a string without string.h and loop is by using sprintf() method. Let's take a look at an example: C #include <stdio.h> int main() { char s[] = "Geeks"; // Temporary buffer char buff[50]; // Finding length of string s int l = sprintf(buff, "%s", s); printf("%d", l); return 0; } Output5Explanation: The sprintf() method returns the number of characters it printed in the string buffer. We could have used the printf() method too but it will output the string in the console.There are a few more methods to find length of a string without string.h and loop in C. Some of them are:Using RecursionIf loops cannot be used, recursion is the technique that can be used to count the number of characters in the string. 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. Comment More infoAdvertise with us Next Article How to Find Length of a String Without string.h and Loop in C? S Sumit Singh Chauhan Improve Article Tags : C Programs C Language C-String Similar Reads Length of a String Without Using strlen() Function in C 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 met 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 How to Match a Pattern in a String in C? Matching a pattern in a string involves searching for a specific sequence of characters within a larger string. In this article, we will learn different methods to efficiently match patterns in a string in C.The most straightforward method to match a string is by using strstr() function. Letâs take 3 min read How to Convert a String to a Char Array in C? In C, the only difference between the string and a character array is possibly the null character '\0' but strings can also be declared as character pointer in which case, its characters are immutable. In this article, we will learn how to convert a string to a char array in C.The most straightforwa 2 min read How to Find the Size of an Array in C? The size of an array is generally considered to be the number of elements in the array (not the size of memory occupied in bytes). In this article, we will learn how to find the size of an array in C.The simplest method to find the size of an array in C is by using sizeof operator. First determine t 2 min read How to Append a Character to a String in C? In this article, we will learn how to append a character to a string using the C program.The most straightforward method to append a character to a string is by using a loop traverse to the end of the string and append the new character manually.C#include <stdio.h> void addChar(char *s, char c 3 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 How to Create a Dynamic Array of Strings in C? In C, dynamic arrays are essential for handling data structures whose size changes dynamically during the program's runtime. Strings are arrays of characters terminated by the null character '\0'. A dynamic array of strings will ensure to change it's size dynamically during the runtime of the progra 3 min read Like