Algorithms_in_C  1.0.0
Set of algorithms implemented in C.
strlen.c File Reference

Program to calculate length of string. More...

#include <assert.h>
#include <string.h>
Include dependency graph for strlen.c:

Functions

int length (const char *str)
 Returns the length of string. More...
 
static void test ()
 Test function. More...
 
int main ()
 Driver Code. More...
 

Detailed Description

Program to calculate length of string.

Author
Du Yuanchao

Function Documentation

◆ length()

int length ( const char *  str)

Returns the length of string.

Parameters
strthe pointer of string.
Returns
the length of string.
16 {
17  int count = 0;
18  for (int i = 0; *(str + i) != '\0'; ++i)
19  {
20  count++;
21  }
22  return count;
23 }

◆ main()

int main ( void  )

Driver Code.

Returns
0 on exit
43 {
44  test();
45  return 0;
46 }
Here is the call graph for this function:

◆ test()

static void test ( void  )
static

Test function.

Returns
void
30 {
31  assert(length("") == strlen(""));
32  assert(length(("a")) == strlen("a"));
33  assert(length("abc") == strlen("abc"));
34  assert(length("abc123def") == strlen("abc123def"));
35  assert(length("abc\0def") == strlen("abc\0def"));
36 }
Here is the call graph for this function:
test
static void test()
Test function.
Definition: strlen.c:29
length
int length(const char *str)
Returns the length of string.
Definition: strlen.c:15