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

Program to calculate length of string using recursion. More...

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

Functions

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

Detailed Description

Program to calculate length of string using recursion.

Author
Du Yuanchao

Function Documentation

◆ length()

int length ( const char *  str)

Returns the length of string using recursion.

Parameters
strthe pointer of string.
Returns
the length of string.
15 { return *str == '\0' ? 0 : 1 + length(++str); }

◆ main()

int main ( void  )

Driver Code.

Returns
0 on exit
35 {
36  test();
37  return 0;
38 }
Here is the call graph for this function:

◆ test()

static void test ( void  )
static

Test function.

Returns
void
22 {
23  assert(length("") == strlen(""));
24  assert(length(("a")) == strlen("a"));
25  assert(length("abc") == strlen("abc"));
26  assert(length("abc123def") == strlen("abc123def"));
27  assert(length("abc\0def") == strlen("abc\0def"));
28 }
Here is the call graph for this function:
length
int length(const char *str)
Returns the length of string using recursion.
Definition: strlen_recursion.c:15
test
static void test()
Test function.
Definition: strlen_recursion.c:21