C Language: Pointers 15

Self-defined function using pointers(Selflen())

Important Question:

C program using pointers, write a user defined function named “selflen()” to find the length of a string?

example:
n=”hello world”
h
e
l
l
o

w
o
r
l
d
Length = 11

Sol:

#include<stdio.h>
void selflen(char *p)
{
	int i=0;
	while(*p!='\0')
	{
		printf("%c\n",*p);
		p++;
		i++;
	}
	printf("Length = %d\n",i);
}
int main()
{
  char n[20];
  printf("Enter any string ");
  gets(n);
  selflen(n);
  /* selflen(&n[0]); */
  return(0);
}

Output:

Enter any string hello world
h
e
l
l
o

w
o
r
l
d
Length = 11