Contact Learn C
Copy

Program 41:To print length of a string using string functions

Program 41:
#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.
  1. 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.
  2.  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'
  3. 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:

Print length of a string using string functions

1 comment:

  1. factorial hundred In the last few days, the “factorial of 100” is one of the top subjects and a lot of maths geeks compute it using voice assistants such as Alexa, Shiri, etc.

    factorial hundred In the last few days, the “factorial of 100” is one of the top subjects and a lot of maths geeks compute it using voice assistants such as Alexa, Shiri, etc.

    factorial hundred In the last few days, the “factorial of 100” is one of the top subjects and a lot of maths geeks compute it using voice assistants such as Alexa, Shiri, etc.

    ReplyDelete

Donate

Download App and Learn when ever you want

Get it on PlayStore
Get it on Amazon App Store
Get it on Aptoide