C Program to Find Length of String

Program 1:
                   To find the length of that string using strlen() function of string.h library


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
    clrscr();
    char str[20], len;
    printf("Enter a string : ");
    gets(str);
    len=strlen(str);
    printf("Length of the string is %d",len);
    getch();
}


Program 2:
              Without Using strlen() function of string.h library


#include<stdio.h
#include<conio.h>

 int StringLength(char[]);/* Function Prototype*/

 void main()
 {
  char str[50];
    int len;
  printf("Enter a string:-");
  gets(str);
  len=StringLength(str);/*Function call*/
  printf("Length of the string is '%d'",len);
  getch();

 }
 int StringLength(char x[]) /*Function Defination*/
 {
    int i=0,count=0;
     while(x[i]!='\0')
      {
         count++;
         i++;

      }
      return count;

 }



Run:

 

No comments:

Post a Comment

100 C Programs with Code and Output

Program 1: C Program To Read Two Numbers And Print The Sum Of Given Two Numbers. Program 2: C Program To Read Three Numbers And Prin...