C Program to Find String Length Using User Defined Function

This C program finds string length in a given string using user defined function and without using string handling function strlen().

C Source Code: String Length Using User Defined Function


#include<stdio.h>

/* Function Prototype */
int mystrlen(char str[30]);

/* Main Function */
int main()
{
 char str[30];
 int i, len;

 printf("Enter string:\n");
 gets(str);

 len = mystrlen(str); /* Function Call */
 printf("Length of given string is: %d", len);

 return 0;
}

/* Function Definition */
int mystrlen(char str[30])
{
 int i, len=0;

 for(i=0;str[i]!='\0';i++)
 {
  len++;
 }

 return(len);
}

Output

Enter string:
Welcome to C ↲
Length of given string is: 12

Note: ↲ represents ENETR key is pressed.