Length of string c code without using string function



Length of string c code without using string function. This c program is used to calculate the length of string. We can calculate length by using in built function in “strlen”, included in string header file i.e string.h. You can see other string function in c and how it is implemented in c program. The below program uses calstr() user made function to work same as strlen().

Length of string c code

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

int calstr(char *);

main( )
{
     char a[20];
     int l;
     printf("enter the string for calculating lenght\n");
     gets(a);
     l = calstr ( a ) ;
     printf ( "\nstring is %s \tlength of string = %d", a, l ) ;
     getch();
}

int calstr ( char *b )
{
     int length = 0 ;
     while ( *b != '\0' )
     {
          length++ ;
          b++ ;
     }
     return ( length ) ;
}

Output of Length of string c code:

Length of string c code without using string function
output : Length of string c code without using string function

We can observe from the above output that while calculating length of string, compiler takes blank space or white space as a single character and its ASCII value is 32.




"Please Do Like Facebook Page and follow us on Twitter so that you can actively participate and develop skills in programming. If you find above post interesting do share the webpage."
This entry was posted in c program and tagged , . Bookmark the permalink.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>