Skip to content

Commit 598630c

Browse files
realDuYuanChaogithub-actionsPanquesito7
authored
Added strlen function (#606)
* added strlen function * updating DIRECTORY.md * Update strings/strlen.c Co-authored-by: David Leal <[email protected]> * Update strings/strlen_recursion.c Co-authored-by: David Leal <[email protected]> Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: David Leal <[email protected]>
1 parent 88726b9 commit 598630c

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

DIRECTORY.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,3 +382,7 @@
382382
* [Shell Sort](https://github.com/TheAlgorithms/C/blob/master/sorting/shell_sort.c)
383383
* [Shell Sort2](https://github.com/TheAlgorithms/C/blob/master/sorting/shell_sort2.c)
384384
* [Stooge Sort](https://github.com/TheAlgorithms/C/blob/master/sorting/stooge_sort.c)
385+
386+
## Strings
387+
* [Strlen](https://github.com/TheAlgorithms/C/blob/master/strings/strlen.c)
388+
* [Strlen Recursion](https://github.com/TheAlgorithms/C/blob/master/strings/strlen_recursion.c)

strings/strlen.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* @file
3+
* @brief Program to calculate length of string.
4+
*
5+
* @author [Du Yuanchao](https://github.com/shellhub)
6+
*/
7+
#include <assert.h>
8+
#include <string.h>
9+
10+
/**
11+
* @brief Returns the length of string
12+
* @param str the pointer of string.
13+
* @return the length of string.
14+
*/
15+
int length(const char *str)
16+
{
17+
int count = 0;
18+
for (int i = 0; *(str + i) != '\0'; ++i)
19+
{
20+
count++;
21+
}
22+
return count;
23+
}
24+
25+
/**
26+
* @brief Test function
27+
* @return void
28+
*/
29+
static void test()
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+
}
37+
38+
/**
39+
* @brief Driver Code
40+
* @returns 0 on exit
41+
*/
42+
int main()
43+
{
44+
test();
45+
return 0;
46+
}

strings/strlen_recursion.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @file
3+
* @brief Program to calculate length of string using recursion.
4+
*
5+
* @author [Du Yuanchao](https://github.com/shellhub)
6+
*/
7+
#include <assert.h>
8+
#include <string.h>
9+
10+
/**
11+
* @brief Returns the length of string using recursion
12+
* @param str the pointer of string.
13+
* @return the length of string.
14+
*/
15+
int length(const char *str) { return *str == '\0' ? 0 : 1 + length(++str); }
16+
17+
/**
18+
* @brief Test function
19+
* @return void
20+
*/
21+
static void test()
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+
}
29+
30+
/**
31+
* @brief Driver Code
32+
* @returns 0 on exit
33+
*/
34+
int main()
35+
{
36+
test();
37+
return 0;
38+
}

0 commit comments

Comments
 (0)