Skip to content
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
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions courses/cunix/ex01/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ LDFLAGS += -Llib

CFLAGS += -Iinclude -Wall -Wextra -Werror -g

ccflags-y := -std=c99

SRCS := $(wildcard src/*.c)

OBJS := $(SRCS:.c=.o)
Expand Down
28 changes: 28 additions & 0 deletions courses/cunix/ex01/src/my_strlen.c
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 ----- */
2 changes: 2 additions & 0 deletions courses/cunix/ex02/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ LDFLAGS += -Llib

CFLAGS += -Iinclude -Wall -Wextra -Werror -g

ccflags-y := -std=c99

SRCS := $(wildcard src/*.c)

OBJS := $(SRCS:.c=.o)
Expand Down
31 changes: 31 additions & 0 deletions courses/cunix/ex02/src/my_strcmp.c
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) {
Copy link

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

while (*(str1) != '\0' && *(str2) != '\0') {
if (*(str1) != *(str2)) {
return *(str1) - *(str2);
}
str1++;
str2++;
}

return *(str1) - *(str2);
}
/* ----- end of function my_strcmp ----- */
8 changes: 4 additions & 4 deletions courses/cunix/ex02/src/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changing tests is not allowed unless there is a bug.

}

int main()
Expand Down
4 changes: 3 additions & 1 deletion courses/cunix/ex03/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ LDFLAGS += -Llib

CFLAGS += -Iinclude -Wall -Wextra -Werror -g

ccflags-y := -std=c99

SRCS := $(wildcard src/*.c)

OBJS := $(SRCS:.c=.o)
Expand All @@ -27,7 +29,7 @@ fclean: clean
re: fclean all

test: re
@(./$(NAME) && echo "Test success" || echo "Test Fails")
@(./$(NAME) && echo "Test Successes" || echo "Test Fails")

debug: $(NAME)
valgrind -v --leak-check=full --track-origins=yes ./$(NAME)
Expand Down
42 changes: 42 additions & 0 deletions courses/cunix/ex03/src/my_strcpy.c
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 ----- */
2 changes: 2 additions & 0 deletions courses/cunix/ex04/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ LDFLAGS += -Llib

CFLAGS += -Iinclude -Wall -Wextra -Werror -g

ccflags-y := -std=c99

SRCS := $(wildcard src/*.c)

OBJS := $(SRCS:.c=.o)
Expand Down
57 changes: 57 additions & 0 deletions courses/cunix/ex04/src/my_atoi.c
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 ----- */
36 changes: 36 additions & 0 deletions courses/cunix/ex04/src/my_itoa.c
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);
Copy link

Choose a reason for hiding this comment

The 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 ----- */
2 changes: 2 additions & 0 deletions courses/cunix/ex05/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ LDFLAGS += -Llib

CFLAGS += -Iinclude -Wall -Wextra -Werror -g

ccflags-y := -std=c99

SRCS := $(wildcard src/*.c)

OBJS := $(SRCS:.c=.o)
Expand Down
47 changes: 47 additions & 0 deletions courses/cunix/ex05/src/my_puts.c
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 ----- */
2 changes: 1 addition & 1 deletion courses/cunix/ex07/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ RM := rm -rf

LDFLAGS += -Llib

CFLAGS += -Iinclude -Wall -Wextra -Werror -g
CFLAGS += -std=c99 -Iinclude -Wall -Wextra -Werror -g

SRCS := $(wildcard src/*.c)

Expand Down
Loading