Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
Iam new to c programming. My friend challenged me to write my own strlen function to find its length. I dont know how to write it. Please help me.

What I have tried:

I have used strlen() but he told not to use it.
Posted
Updated 17-Dec-21 6:57am

1 solution

It is simple I have written the code for you check it.
C++
#include<stdio.h>
int my_strlen(char *str)
{

	int rv;
	
	
	for (rv=0; str[rv] != 0; rv++) 
        {
       
         }
	return rv;
}
int main(int argc, char const *argv[])
{
	char str[10]={'H','e','l','l','o','\0'};
	printf("\nLength is %d\n",my_strlen(str));
	return 0;
}


You can take input using scanf.
 
Share this answer
 
v3
Comments
Member 14985242 5-Nov-20 22:17pm    
Thanks that works.
Rick York 6-Nov-20 0:33am    
"char str[10]={'H','e','l','l','o','\0'};"

Really?
CPallini 6-Nov-20 2:04am    
My 5.
Note, you may get rid of rv and return directly i.
Scholar14 6-Nov-20 23:36pm    
I have changed the code. Please dont rate 1 *.
CPallini 7-Nov-20 16:22pm    
I voted 5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900