Steps:-
- read a string from keyboard
- pass string to a function as pointer
- increment the string pointer and i till it encounters null ('\0') and while fails
- return the length to main program
/* C program */
#include<stdio.h>
#include<stdlib.h>
/* program- calculate the lengh of the
string (strlen())
Author:- Surendra Patil
Date:- july 11 2013
*/
int str_len(char *);
int main() {
char *str = NULL;
/* allocate soutput:-ome memory */
str =
(char*)malloc(20*sizeof(char));
printf("Enter the string
\n");
scanf("%s",str);
printf("Length of the
string = %d\n",str_len(str));
return 0;
}
/* Function returns the length of the string */
int str_len (char *string)
{
int i =0;
/* increment the pointer till end of
the string */
while(*string++)
++i;
return i;
}
input:-
Enter the string :-
Surendra
output:-
Length of the string = 8
Comments