File tree Expand file tree Collapse file tree 3 files changed +88
-0
lines changed Expand file tree Collapse file tree 3 files changed +88
-0
lines changed Original file line number Diff line number Diff line change 382
382
* [ Shell Sort] ( https://github.com/TheAlgorithms/C/blob/master/sorting/shell_sort.c )
383
383
* [ Shell Sort2] ( https://github.com/TheAlgorithms/C/blob/master/sorting/shell_sort2.c )
384
384
* [ 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 )
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments