Program 41:
Here we used string.h header for using string library functions such as strlen,strcpy,gets,strcmp etc.
Output:
#include<stdio.h>
#include<string.h>
main()
{
char str[100];
int len;
printf("Enter a string to know the length\n");
gets(str);
len=strlen(str);
printf("The length of %s is %d\n",str,len);
}
Explanation:Here we used string.h header for using string library functions such as strlen,strcpy,gets,strcmp etc.
- This program starts with initializing :
- str[100] → To store string with length of 100 which means it can store 100 letters
- len→ To store length of given string.
printf("Enter a string to know the length\n"); gets(str);To take input from user. Difference between gets(str) ,and taking input using scanf is that,using gets we can enter a string having spaces in between words and takes Enter as end of the string.But scanf wont take 'space' as a character.For example if we enter a string 'hello world' using :-- scanf- It will take only 'hello'
- gets- It will take 'hello world'
len=strlen(str); printf("The length of %s is %d\n",str,len);strlen calculates the length of str and final length value is printed as output.
Output:



