Write a program to find the length of the string using printf() fuction.

#include<stdio.h>
#include<conio.h>
int main()
{
	char nm[20];
	int l;
	printf("Enter string:");
	scanf("%s",nm);
	l=printf(nm);
	printf("\n Length=%d",l);
	getch();
}

/*The printf fuction returns the length of the string entered,In the above program the string entered
is Hello, Length of the string is 5.*/ 

The program first declares a character array nm of size 20 and an integer variable l to hold the length of the string.

It then prompts the user to enter a string with the message “Enter string:” and scans it into the character array nm using the scanf() function.

The length of the string is then computed using the printf() function, which returns the number of characters printed to the console. In this case, the string is passed as a parameter to printf() and printed to the console, which causes it to return the length of the string.

Finally, the length of the string is assigned to the l variable and printed to the console with the message “Length=<length of the string>” using the printf() function.

I hope that helps! Let me know if you have any further questions.

Thanks

Write a program to find the length of the string using printf() fuction.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top