C Program to Print the Length of a String using Pointers Last Updated : 05 Dec, 2024 Comments Improve Suggest changes Like Article Like Report 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 the string. Let's take a look at an example: C #include <stdio.h> int findLen(char *s) { char* last = s; // Move the pointer to the last while(*last++); return last - s - 1; } int main() { char s[] = "Geeks"; // Find and print the string length printf("%d", findLen(s)); return 0; } Output5There are two other methods in C to find the length of the string using pointers:By CountingIncrement the pointer to the string till it points to the null character while keeping the count of characters encountered. C++ #include <stdio.h> int findLen(char *s) { int c = 0; // Counting the characters while(*s++) c++; return c; } int main() { char s[] = "Geeks"; // Find and print the string length printf("%d", findLen(s)); return 0; } Output5Using Pointer ArithmeticThis technique is only useful when the string is declared as character array and is declared in the same scope. Increment 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; } Output5 Comment More infoAdvertise with us Next Article C Program to Print the Length of a String using Pointers M mohitw16 Follow Improve Article Tags : Misc C Programs C Language DSA C-Pointers C Strings Programs +2 More Practice Tags : Misc Similar Reads 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 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 C Program to Compare Two Strings Using Pointers In C, two strings are generally compared character by character in lexicographical order (alphabetical order). In this article, we will learn how to compare two strings using pointers.To compare two strings using pointers, increment the pointers to traverse through each character of the strings whil 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 Print the First Letter of Each Word In a string that contains multiple words, each word is separated by a whitespace. In this article, we will learn how to print the first letter of each word using a C program.The simplest method to print the first letter of each word is by using a loop. Letâs take a look at an example:C#include <s 3 min read Array of Pointers to Strings in C In C, arrays are data structures that store data in contiguous memory locations. Pointers are variables that store the address of data variables. We can use an array of pointers to store the addresses of multiple elements. In this article, we will learn how to create an array of pointers to strings 2 min read Lex program to find the length of the longest word Lex is a computer program that generates lexical analyzers. Lex reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in the C programming language. The commands for executing the lex program are: lex abc.l (abc is the file name) cc lex.yy.c -efl ./a.ou 1 min read C Program to Find the Size of int, float, double and char Write a C program to find the size of the data types: int, float, double, and char in bytes and print it on the output screen.ExamplesInput: charOutput: Size of char: 1 byteInput: intOutput:Size of int: 4 bytesDifferent Methods to Find the Size of int, float, double and char in CWe can find the size 4 min read C Program to find size of a File Given a text file, find its size in bytes. Examples: Input : file_name = "a.txt" Let "a.txt" contains "geeks" Output : 6 Bytes There are 5 bytes for 5 characters then an extra byte for end of file. Input : file_name = "a.txt" Let "a.txt" contains "geeks for geeks" Output : 16 Bytes The idea is to us 1 min read Like