C program to print the length of a String using %n format specifier
Given string str. The task is to find the length of the string using %n format specifier
Examples:
Input: Geeks For Geeks Output: 15 Input: Geeks Output: 5
Appropach:
To find the length of string, we use special format specifier “%n” in printf function. In C printf(), %n is a special format specifier which instead of printing something causes printf() to load the variable pointed by the corresponding argument with a value equal to the number of characters that have been printed by printf() before the occurrence of %n.
Below is the implementation of the above approach:
// C program to print // the length of a String // using %n format specifier #include <stdio.h> // Driver code int main() { char str[100] = "Geeks for Geeks"; int len = 0; printf("%s%n", str, &len;); printf(" = %d", len); return 0; } |
Geeks for Geeks = 15
Recommended Posts:
- Print string of odd length in 'X' format
- Lex program to find the Length of a String
- Program for length of a string using recursion
- C program to find the length of a string
- Program to print all substrings of a given string
- C Program to print all permutations of a given string
- Write a program to print all permutations of a given string
- Java Program to print distinct permutations of a string
- Program to print characters present at prime indexes in a given string
- Minimum length of the sub-string whose characters can be used to form a palindrome of length K
- C program to write an image in PGM format
- C program to invert (making negative) an image content in PGM format
- Find length of longest subsequence of one string which is substring of another string
- Minimum length of string having all permutation of given string.
- Check if a string contains a palindromic sub-string of even length
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





