-
Notifications
You must be signed in to change notification settings - Fork 69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Solution | ex01-05, ex07-08 #59
Closed
Closed
Changes from 19 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
537edea
Add c99 flag
nksazonov 7a6c0fc
add my_strlen implementation
nksazonov ee0b983
add c99 flag
nksazonov 4097224
Generalize string comparison test results
nksazonov a69b0c5
Implement my_strcmp function
nksazonov 574e2f8
Add c99 flag
nksazonov 479eb2a
Implement my_strcpy
nksazonov 3a1fcb9
Make test messages more consistent
nksazonov 25e2a46
remove unnecessary var
nksazonov bbe0ef1
removed unnecessary vars
nksazonov 49d0890
Implement my_atoi, my_itoa
nksazonov a93e5f6
Add c99 flag
nksazonov 4e31b2f
Add c99 flag
nksazonov 8fb8d7a
Implement my_puts
nksazonov 283ec22
Add c99 flag
nksazonov 0a16aef
Fix void pointer print in printf
nksazonov ef1fae7
Done linked list
nksazonov 85445c5
Add c99 flag
nksazonov ddb53e7
Done binary tree
nksazonov cab0fa5
Fix brackets style
nksazonov 3050fbd
Fix removed typedef from .c file
nksazonov fc291ba
Fix memory leaks, wrong function signature
nksazonov d1bef87
Fix typedef removed, nice usage of function pointers
nksazonov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* ===================================================================================== | ||
* | ||
* Filename: my_strlen.c | ||
* | ||
* Description: own implementation of std strel function | ||
* | ||
* Version: 1.0 | ||
* Created: 08/20/2021 12:48:09 PM | ||
* Revision: none | ||
* Compiler: gcc | ||
* | ||
* Author: Nikita Sazonov (ns), [email protected] | ||
* Company: | ||
* | ||
* ===================================================================================== | ||
*/ | ||
|
||
unsigned int my_strlen(char *str) | ||
{ | ||
unsigned int len = 0; | ||
while (*(str++) != '\0') | ||
{ | ||
len++; | ||
} | ||
return len; | ||
} | ||
/* ----- end of function my_strlen ----- */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* ===================================================================================== | ||
* | ||
* Filename: my_strcmp.c | ||
* | ||
* Description: std strcmp implementation | ||
* | ||
* Version: 1.0 | ||
* Created: 08/20/2021 04:53:28 PM | ||
* Revision: none | ||
* Compiler: gcc | ||
* | ||
* Author: Nikita Sazonov (ns), [email protected] | ||
* Company: | ||
* | ||
* ===================================================================================== | ||
*/ | ||
|
||
|
||
int my_strcmp(char *str1, char *str2) { | ||
while (*(str1) != '\0' && *(str2) != '\0') { | ||
if (*(str1) != *(str2)) { | ||
return *(str1) - *(str2); | ||
} | ||
str1++; | ||
str2++; | ||
} | ||
|
||
return *(str1) - *(str2); | ||
} | ||
/* ----- end of function my_strcmp ----- */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,15 +10,15 @@ void test_small() | |
assert(my_strcmp("", "") == 0); | ||
assert(my_strcmp("A", "A") == 0); | ||
assert(my_strcmp("AB", "AB") == 0); | ||
assert(my_strcmp("AB", "AC") == -1); | ||
assert(my_strcmp("AB", "AA") == 1); | ||
assert(my_strcmp("AB", "AD") == -1); | ||
assert(my_strcmp("AB", "AC") < 0); | ||
assert(my_strcmp("AB", "AA") > 0); | ||
assert(my_strcmp("AB", "AD") < 0); | ||
} | ||
|
||
void test_long() | ||
{ | ||
assert(my_strcmp("HELLO WORLD", "HELLA WORLD") > 0); | ||
assert(my_strcmp("HELLO WORLD", "HELL WORLD") == 1); | ||
assert(my_strcmp("HELLO WORLD", "HELL WORLD") > 0); | ||
Comment on lines
+13
to
+21
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. changing tests is not allowed unless there is a bug. |
||
} | ||
|
||
int main() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* ===================================================================================== | ||
* | ||
* Filename: my_strcpy.c | ||
* | ||
* Description: own std strcpy implementation | ||
* | ||
* Version: 1.0 | ||
* Created: 08/20/2021 08:00:40 PM | ||
* Revision: none | ||
* Compiler: gcc | ||
* | ||
* Author: Nikita Sazonov (ns), [email protected] | ||
* Company: | ||
* | ||
* ===================================================================================== | ||
*/ | ||
|
||
/* | ||
* === FUNCTION ====================================================================== | ||
* Name: my_strcpy | ||
* Description: std strcpy implementation. | ||
* Note: the user must ensure that 'dest' buffer has enough space to have 'src' copied into it, otherwise undefined behavior! | ||
* ===================================================================================== | ||
*/ | ||
|
||
#include <stddef.h> | ||
|
||
char *my_strcpy(char *dest, char *src) | ||
{ | ||
size_t i = 0; | ||
|
||
while (src[i] != '\0') | ||
{ | ||
dest[i] = src[i]; | ||
i++; | ||
} | ||
|
||
dest[i] = '\0'; | ||
return dest; | ||
} | ||
/* ----- end of function my_strcpy ----- */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* ===================================================================================== | ||
* | ||
* Filename: my_atoi.c | ||
* | ||
* Description: own implementation of std atoi function | ||
* | ||
* Version: 1.0 | ||
* Created: 08/20/2021 09:37:38 PM | ||
* Revision: none | ||
* Compiler: gcc | ||
* | ||
* Author: Nikita Sazonov (ns), [email protected] | ||
* Company: | ||
* | ||
* ===================================================================================== | ||
*/ | ||
|
||
|
||
/* | ||
* === FUNCTION ====================================================================== | ||
* Name: my_atoi | ||
* Description: converts array of chars 'nptr' to a number up to a non-digit character. Any leading spaces are skipped, the sign char is read if present. If a string can't be parsed to a number, 0 is returned. | ||
* ===================================================================================== | ||
*/ | ||
int my_atoi(const char *nptr) | ||
{ | ||
int num = 0, sign = 1, i = 0; | ||
|
||
// skip leading spaces | ||
while (nptr[i] == ' ') | ||
{ | ||
i++; | ||
} | ||
|
||
// read the sign of a number if any | ||
if (nptr[i] == '-' || nptr[i] == '+') | ||
{ | ||
if (nptr[i] == '-') | ||
{ | ||
sign = -1; | ||
} | ||
i++; | ||
} | ||
|
||
// read digits one by one | ||
while ('0' <= nptr[i] && nptr[i] <= '9') | ||
{ | ||
num = num * 10 + (nptr[i] - '0'); | ||
i++; | ||
} | ||
|
||
num *= sign; | ||
|
||
return num; | ||
} | ||
/* ----- end of function my_atoi ----- */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* ===================================================================================== | ||
* | ||
* Filename: my_itoa.c | ||
* | ||
* Description: own implementation of itoa function | ||
* | ||
* Version: 1.0 | ||
* Created: 08/20/2021 10:46:17 PM | ||
* Revision: none | ||
* Compiler: gcc | ||
* | ||
* Author: Nikita Sazonov (ns), [email protected] | ||
* Company: | ||
* | ||
* ===================================================================================== | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <limits.h> | ||
#include <stdlib.h> | ||
|
||
/* | ||
* === FUNCTION ====================================================================== | ||
* Name: my_itoa | ||
* Description: converts 'nmb' to array of char | ||
* ===================================================================================== | ||
*/ | ||
char *my_itoa(int nmb) | ||
{ | ||
// using malloc to allocate memory from the heap | ||
char *str = malloc(sizeof nmb * CHAR_BIT + 1 + 1); | ||
sprintf(str, "%d", nmb); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. weeeeeell, it does not count :D |
||
return str; | ||
} | ||
/* ----- end of function my_itoa ----- */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* ===================================================================================== | ||
* | ||
* Filename: my_puts.c | ||
* | ||
* Description: own implementation of std puts function using only write | ||
* | ||
* Version: 1.0 | ||
* Created: 08/20/2021 11:26:08 PM | ||
* Revision: none | ||
* Compiler: gcc | ||
* | ||
* Author: Nikita Sazonov (ns), [email protected] | ||
* Company: | ||
* | ||
* ===================================================================================== | ||
*/ | ||
|
||
#include <stdio.h> | ||
|
||
/* | ||
* === FUNCTION ====================================================================== | ||
* Name: my_puts | ||
* Description: writes the string s and a trailing newline to stdout. | ||
* ===================================================================================== | ||
*/ | ||
int my_puts(const char *s) | ||
{ | ||
int i = 0; | ||
|
||
while (s[i] != '\0') | ||
{ | ||
if (putchar(s[i]) == EOF) | ||
{ | ||
return EOF; | ||
} | ||
i++; | ||
} | ||
|
||
if (putchar('\n') == EOF) | ||
{ | ||
return EOF; | ||
} | ||
|
||
return 1; | ||
} | ||
/* ----- end of function my_puts ----- */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
brackets style is not consistent with other files