C Program to Print the Length of a String using Pointers Last Updated : 05 Dec, 2024 Summarize Comments Improve Suggest changes Share 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 : C Language C-Pointers C Strings Programs Similar Reads Program to Reverse a String using Pointers Given a string, the task is to reverse this String using pointers. Examples:Input: GeeksOutput: skeeGInput: GeeksForGeeksOutput: skeeGroFskeeGApproach: This method involves taking two pointers, one that points at the start of the string and the other at the end of the string. The characters are then 2 min read C program to count number of vowels and consonants in a String Given a string and write a C program to count the number of vowels and consonants in this string. Examples: Input: str = "geeks for geeks" Output: Vowels: 5 Consonants: 8 Input: str = "abcdefghijklmnopqrstuvwxyz" Output: Vowels: 5 Consonants: 21Using For LoopsTake the string as inputTake each charac 4 min read Output of C programs | Set 64 (Pointers) Prerequisite : Pointers in C Question 1 : What will be the output of following program? C #include "stdio.h" int main() { char a[] = { 'A', 'B', 'C', 'D' }; char* ppp = &a[0]; *ppp++; // Line 1 printf("%c %c ", *++ppp, --*ppp); // Line 2 } OPTIONS: a)C B b)B A c)B C d)C AOUTPUT: (d) C AExplanati 3 min read Write a C program to print "Geeks for Geeks" without using a semicolon First of all we have to understand how printf() function works. Prototype of printf() function is: int printf( const char *format , ...) Parameter format: This is a string that contains a text to be written to stdout.Additional arguments: ... (Three dots are called ellipses) which indicates the vari 2 min read C Program to count the Number of Characters in a File Counting the number of characters is important because almost all the text boxes that rely on user input have certain limitations on the number of characters that can be inserted. For example, the character limit on a Facebook post is 63,206 characters. Whereas, for a tweet on Twitter the character 2 min read Like