C program to find string length with and without strlen


                                          As we explaining earlier use %s to enter entire string in character array. Now to count the length of the string C programming provide the string handling function strlen() which return the length of the string. this all string function are available in <string.h> header file

Syntax: 
                                        <int_variable> = strlen(<string>);

Example:
                                         len=strlen(name);

                                          In which we need to store the length of the string in integer variable.As per above example length of the string name store in len variable  



C program to find length of string

#include<stdio.h>
#include<string.h>

void main()
{
        int len;
        char name[50];
        clrscr();

        printf("Enter string : ");
        scanf("%s",&name);

        len=strlen(name);

        printf("Length of the string is : %d",len);
        getch();
}


c program to find string length with strlen function



                                       To find the length of the string without using string function. For that we need to use the loop and one integer variable which count the length. In which loop continue till it not found the null value in character array and also every time integer variable incremented by 1.

C program to find length without string function


#include<stdio.h>

void main()
{
       int i,count=0;
       char name[50];
       clrscr();
       printf("Enter a String:");
       scanf("%s",&name);

       for(i=0;name[i]!='\0';i++)
       {
              count++;
       }

       printf("Length of string is : %d",count);
       getch();
}



c program to find string length without strlen function


0 comments:

Post a Comment