Created
October 12, 2017 04:45
-
-
Save khayyamsaleem/d15846b47f62caa34426991ca36042d6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
CFLAGS = -I../../include -Wall -Werror --pedantic -std=c99 | |
SRC = my_alpha.c my_char.c my_str.c my_strlen.c my_revstr.c my_int.c my_num_base.c my_strindex.c my_strrindex.c my_strfind.c my_strrfind.c my_strcmp.c my_strncmp.c my_strcpy.c my_strncpy.c my_strcat.c my_strdup.c my_strconcat.c my_strnconcat.c my_atoi.c | |
OBJS = $(SRC:.c=.o) | |
LIB = libmy.a | |
CC = gcc | |
all: $(OBJS) | |
ar -rc $(LIB) $(OBJS) | |
ranlib $(LIB) | |
mv $(LIB) ../../lib | |
clean: | |
rm -f $(OBJS) | |
fclean: clean | |
rm -f ../../lib/$(LIB) | |
re: | |
make fclean all |
This file contains hidden or 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
#include "my.h" | |
void my_alpha(){ | |
int i; | |
for(i = 65; i <= 90; i++){ | |
my_char(i); | |
} | |
} |
This file contains hidden or 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
#include "my.h" | |
/** | |
* returns int represented by ascii string | |
* handles negs | |
* ignores preceding chars and trailing numbers | |
* and chars | |
* does not allocate memory | |
*/ | |
int my_atoi(char *s){ | |
int sign = 1; | |
int acc = 0; /*accumulator*/ | |
if (s == NULL) | |
return 0; | |
/* while char is not a digit and char* | |
* hasn't terminated */ | |
while(!('0' <= *s && *s <= '9') && *s != '\0'){ | |
if (*s == '-') /*found neg sign */ | |
sign *= -1; /*negate previous sign */ | |
s++; /* increment char ptr */ | |
} | |
while(*s != '\0' && | |
('0' <= *s && *s <= '9')){ /* char is a dig */ | |
acc *= 10; | |
acc += *s - '0'; /* distance from 0 = val */ | |
s++; /* increment char ptr */ | |
} | |
return sign*acc; | |
} |
This file contains hidden or 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
#include "my.h" | |
void my_char(char c){ | |
write(1, &c, 1); | |
} |
This file contains hidden or 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
#include "my.h" | |
void my_int(int c){ | |
int n = c; | |
int temp =c; | |
int count; | |
int ind; | |
if(c<0) | |
my_char('-'); | |
if(c==0) | |
my_char('0'); | |
else{ | |
/*set counter to zero so temp can be 0 */ | |
for(count=0; count == 0 || n != 0; count++) | |
n /= 10; | |
count--; | |
while(count>=0){ | |
temp = c; | |
for(ind = 0; ind < count; ind++){ | |
temp /= 10; | |
} | |
temp %= 10; | |
if(temp<0) | |
temp*=-1; | |
my_char(temp + '0'); | |
count--; | |
} | |
} | |
} |
This file contains hidden or 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
#include "my.h" | |
/** | |
* Prints a number using the length of the string as the base and the contents as the alphabet | |
* (9, "RFTM") -> "FT" | |
*/ | |
void my_num_base(int a, char *s){ | |
int base; | |
long num; | |
int i = 0; | |
char out[32]; | |
/*if string is null*/ | |
if (s == NULL || *s == '\0'){ | |
my_str("Error: string was null"); | |
return; | |
} | |
base = my_strlen(s); | |
num = a; /* to account for min_int */ | |
/* if number is negative */ | |
if (num < 0){ | |
my_char('-'); | |
num = -num; | |
} | |
/* if number is zero */ | |
if (num == 0){ | |
my_char(s[0]); | |
return; | |
} | |
/* if base 1, then essentially tally marks */ | |
if (base == 1){ | |
for(i = 0; i < num; i++) | |
my_char(s[0]); | |
return; | |
} | |
/* start at 1th cell bc base 1 case has been handled */ | |
while (num > 0){ | |
out[i++] = s[num % base]; | |
num /= base; | |
} | |
out[i] = '\0'; | |
/* output reversed, must fix */ | |
my_revstr(out); | |
my_str(out); | |
} |
This file contains hidden or 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
#include "my.h" | |
int my_revstr(char* s){ | |
int l; /* length 8 */ | |
int f; /* forward pointer */ | |
int b; /* backward pointer */ | |
char t; /* temp char */ | |
if(s==NULL){ | |
/*null check, return ridiculous val */ | |
return -1; | |
} | |
l = my_strlen(s); /* get length */ | |
f = 0; | |
b = l-1; /* stopping condition for backwards iterator */ | |
while(f!=b && f<b){ /* repeat until indices are not the same */ | |
t = *(s+b); | |
*(s+b) = *(s+f); | |
*(s+f) = t; /* store char in temp to pointer at correct position */ | |
f++; /* increment forward pointer */ | |
b--; /* decrement backward pointer */ | |
} | |
return l; | |
} |
This file contains hidden or 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
#include "my.h" | |
/*Prints a string. If string is null or empty, print nothing */ | |
void my_str(char* c){ | |
if (c!= NULL){ | |
while(*c !='\0'){ | |
my_char(*c); | |
c++; | |
} | |
} | |
} |
This file contains hidden or 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
#include "my.h" | |
/** | |
* copies src onto end of dst and returns dst | |
*/ | |
char *my_strcat(char *dst, char *src){ | |
char *concat; | |
if(dst == NULL) | |
return NULL; | |
concat = dst; | |
while(*concat != '\0') | |
concat++; | |
my_strcpy(concat, src); | |
return dst; | |
} |
This file contains hidden or 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
#include "my.h" | |
/** | |
* compares strings by ascii value | |
* if a == b, -> 0 | |
* if a < b -> neg num | |
* if a > b -> pos num | |
* null < any str | |
*/ | |
int my_strcmp(char *x, char *y){ | |
int a; | |
int b; | |
int min; | |
a = my_strlen(x); | |
b = my_strlen(y); | |
min = (a > b) ? a : b; | |
return my_strncmp(x, y, min); | |
} |
This file contains hidden or 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
#include "my.h" | |
/** | |
* Allocates new memory | |
* copies concatenated strings into new memory | |
* returns pointer to location of concatenated strings | |
* if both null, return null | |
*/ | |
char *my_strconcat(char *a, char *b){ | |
int a_length; | |
int b_length; | |
char *out; | |
if (a == NULL && b == NULL) | |
return NULL; | |
if (a == NULL) | |
return my_strdup(b); | |
if (b == NULL) | |
return my_strdup(a); | |
a_length = my_strlen(a); | |
b_length = my_strlen(b); | |
out = malloc(a_length + b_length + 1); | |
my_strcpy(out, a); | |
my_strcpy(out + a_length, b); | |
return out; | |
} |
This file contains hidden or 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
#include "my.h" | |
/** | |
* copies chars from src into dst, | |
* overwriting any content already | |
* in dst. always copies a '/0' | |
* does not allocate any new memory | |
*/ | |
char *my_strcpy(char *dst, char *src){ | |
int i; | |
if (dst == NULL) | |
return NULL; | |
if (src == NULL) | |
return dst; | |
i = 0; | |
while(src[i] != '\0'){ | |
dst[i] = src[i]; | |
i++; | |
} | |
dst[i] = '\0'; | |
return dst; | |
} |
This file contains hidden or 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
#include "my.h" | |
/** | |
* Allocates new memory | |
* copies str into that new memory | |
* returns pointer to that new string | |
*/ | |
char *my_strdup(char *str){ | |
int length; | |
char *copy; | |
if (str == NULL) | |
return NULL; | |
length = my_strlen(str); | |
copy = malloc(length + 1); | |
my_strcpy(copy, str); | |
return copy; | |
} |
This file contains hidden or 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
#include "my.h" | |
/** | |
* Returns a pointer to the first | |
* char in the string which matches | |
*/ | |
char *my_strfind(char *s, char c){ | |
int i; | |
i = my_strindex(s, c); | |
if(i == -1 || s == NULL) | |
return NULL; | |
return s + i; | |
} |
This file contains hidden or 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
#include "my.h" | |
/** | |
* returns index of first instance of a given | |
* character in a given string. returns -1 if | |
* none found | |
*/ | |
int my_strindex(char *s, char c){ | |
int length; | |
int i; | |
/*null checks*/ | |
if(s == NULL) | |
return -1; | |
length = my_strlen(s); | |
for(i = 0; i < length; i++){ | |
if(c == s[i]) | |
return i; | |
} | |
return -1; | |
} |
This file contains hidden or 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
#include "my.h" | |
int my_strlen(char* c){ | |
int l; | |
l=0; | |
if(c ==NULL){ | |
l =-1; | |
}else { | |
while(*c!= '\0'){ | |
l++; | |
c++; | |
} | |
} | |
return l; | |
} |
This file contains hidden or 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
#include "my.h" | |
/** | |
* same as my_strcmp except only compares n chars | |
* or until the end of the string has been reached | |
*/ | |
int my_strncmp(char *x, char *y, int n){ | |
if (x == NULL && y == NULL) | |
return 0; | |
if (x == NULL) | |
return -1; | |
if (y == NULL) | |
return 1; | |
if (n < 0) | |
return 0; | |
while(n > 0 && *x == *y){ | |
x++; | |
y++; | |
n--; | |
} | |
return (n == 0) ? (0) : (*x - *y); | |
} |
This file contains hidden or 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
#include "my.h" | |
/** | |
* same as my_strconcat except copies all of a | |
* and then n chars or all of b, whichever shorter | |
*/ | |
char *my_strnconcat(char *a, char *b, int n){ | |
int a_length = my_strlen(a); | |
int b_length = my_strlen(b); | |
char *out; | |
int length; | |
if(n < 0){ | |
n = 0; | |
} | |
if (a == NULL && b == NULL) | |
return NULL; | |
if (a == NULL){ | |
length = (b_length < n) ? b_length : n; | |
out = malloc(length + 1); | |
my_strncpy(out, b, length); | |
out[length] = '\0'; | |
return out; | |
} | |
if (b == NULL) | |
return my_strdup(a); | |
b_length = (b_length < n) ? b_length : n; | |
out = malloc(a_length + b_length + 1); | |
my_strcpy(out, a); | |
my_strncpy(out + a_length, b, b_length); | |
out[a_length + b_length] = '\0'; | |
return out; | |
} |
This file contains hidden or 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
#include "my.h" | |
/** | |
* Same as my_strcpy except only copies | |
* n chars or until the end of src | |
*/ | |
char *my_strncpy(char *dst, char *src, int n){ | |
int i; | |
if (src == NULL || dst == NULL) | |
return dst; | |
i = 0; | |
while(src != '\0' && n > 0){ | |
dst[i] = src[i]; | |
i++; | |
n--; | |
} | |
dst[i] = '\0'; | |
return dst; | |
} |
This file contains hidden or 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
#include "my.h" | |
/** | |
* Returns a pointer to the last char | |
* in the string which matches. returns | |
* null if the letter is not found or if | |
* input string is null | |
*/ | |
char *my_strrfind(char *s, char c){ | |
int i; | |
i = my_strrindex(s, c); | |
if(s == NULL || i == -1) | |
return NULL; | |
return s + i; | |
} |
This file contains hidden or 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
#include "my.h" | |
int my_strrindex(char *s, char c){ | |
int length; | |
int i; | |
if(s == NULL) | |
return -1; | |
length = my_strlen(s); | |
for(i = length - 1; i >= 0; i--){ | |
if(c == s[i]){ | |
return i; | |
} | |
} | |
return -1; | |
} |
This file contains hidden or 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
#include "my.h" | |
#include <stdio.h> | |
char *my_vect2str(char **x) | |
{ | |
if (x == NULL) | |
return NULL; | |
if (*x == NULL) | |
return my_strdup(""); | |
int len = 1; | |
for (char **y = x; *y != NULL; y++) { | |
len += 1 + my_strlen(*y); | |
} | |
len--; // last space | |
char *s = calloc(len, 1); | |
for (char **y = x; *y != NULL; ++y) { | |
my_strcat(s, *y); | |
s[my_strlen(s)] = ' '; | |
} | |
s[len - 1] = '\0'; | |
return s; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment