From 2d212866ecf508c210b4a2ec0394abefd7d69b46 Mon Sep 17 00:00:00 2001 From: Supermjork Date: Tue, 22 Aug 2023 23:14:13 +0300 Subject: [PATCH] Nothing to see here --- _printf.c | 44 ++++++ _putchar.c | 12 ++ exclusive.c | 39 +++++ main.h | 57 ++++++-- README.md => proven-point/README.md | 0 itoa.c => proven-point/itoa.c | 0 proven-point/main.h | 18 +++ memset.c => proven-point/memset.c | 0 printf.c => proven-point/printf.c | 0 puts.c => proven-point/puts.c | 0 strcpy.c => proven-point/strcpy.c | 0 proven-point/strings.c | 84 +++++++++++ proven-point/tests/a | Bin 0 -> 16552 bytes {tests => proven-point/tests}/append.c | 0 {tests => proven-point/tests}/char_print.c | 0 {tests => proven-point/tests}/chonk.c | 0 {tests => proven-point/tests}/hello.c | 0 {tests => proven-point/tests}/itoa.c | 0 {tests => proven-point/tests}/num_test.c | 0 {tests => proven-point/tests}/puts.c | 0 {tests => proven-point/tests}/str_print.c | 0 {tests => proven-point/tests}/strcpy.c | 0 {tests => proven-point/tests}/strlen.c | 0 {tests => proven-point/tests}/test.sh | 0 running-out-of-names.c | 154 ++++++++++++++++++++ special.c | 121 ++++++++++++++++ strings.c | 160 +++++++++++++-------- strs2.c | 122 ++++++++++++++++ 28 files changed, 735 insertions(+), 76 deletions(-) create mode 100644 _printf.c create mode 100644 _putchar.c create mode 100644 exclusive.c rename README.md => proven-point/README.md (100%) rename itoa.c => proven-point/itoa.c (100%) create mode 100644 proven-point/main.h rename memset.c => proven-point/memset.c (100%) rename printf.c => proven-point/printf.c (100%) rename puts.c => proven-point/puts.c (100%) rename strcpy.c => proven-point/strcpy.c (100%) create mode 100644 proven-point/strings.c create mode 100755 proven-point/tests/a rename {tests => proven-point/tests}/append.c (100%) rename {tests => proven-point/tests}/char_print.c (100%) rename {tests => proven-point/tests}/chonk.c (100%) rename {tests => proven-point/tests}/hello.c (100%) rename {tests => proven-point/tests}/itoa.c (100%) rename {tests => proven-point/tests}/num_test.c (100%) rename {tests => proven-point/tests}/puts.c (100%) rename {tests => proven-point/tests}/str_print.c (100%) rename {tests => proven-point/tests}/strcpy.c (100%) rename {tests => proven-point/tests}/strlen.c (100%) rename {tests => proven-point/tests}/test.sh (100%) create mode 100644 running-out-of-names.c create mode 100644 special.c create mode 100644 strs2.c diff --git a/_printf.c b/_printf.c new file mode 100644 index 0000000..23e61fb --- /dev/null +++ b/_printf.c @@ -0,0 +1,44 @@ +#include "main.h" +/** + * _printf - printf but slow fr + * @format: identifier. + * Return: length. + */ +int _printf(const char * const format, ...) +{ + convert_match m[] = { + {"%s", printf_string}, {"%c", printf_char}, + {"%%", printf_37}, {"%i", printf_int}, + {"%d", printf_dec}, {"%r", printf_srev}, + {"%R", printf_rot13}, {"%b", printf_bin}, {"%u", printf_unsigned}, + {"%o", printf_oct}, {"%x", printf_hex}, {"%X", printf_HEX}, + {"%S", printf_exclusive_string}, {"%p", printf_pointer} + }; + + va_list args; + int i = 0, j, len = 0; + + va_start(args, format); + if (format == NULL || (format[0] == '%' && format[1] == '\0')) + return (-1); +Here: + while (format[i] != '\0') + { + j = 13; + while (j >= 0) + { + if (m[j].id[0] == format[i] && m[j].id[1] == format[i + 1]) + { + len += m[j].f(args); + i = i + 2; + goto Here; + } + j--; + } + _putchar(format[i]); + len++; + i++; + } + va_end(args); + return (len); +} diff --git a/_putchar.c b/_putchar.c new file mode 100644 index 0000000..81ffcfb --- /dev/null +++ b/_putchar.c @@ -0,0 +1,12 @@ +#include "main.h" + +/** + * _putchar - print character + * @c: character + * + * Return: 1 idk + */ +int _putchar(char c) +{ + return (write(1, &c, 1)); +} diff --git a/exclusive.c b/exclusive.c new file mode 100644 index 0000000..66db664 --- /dev/null +++ b/exclusive.c @@ -0,0 +1,39 @@ +#include "main.h" +/** + * printf_exclusive_string - print exclusuives string. + * @val: argumen t. + * Return: the length of the string. + */ + +int printf_exclusive_string(va_list val) +{ + char *s; + int i, len = 0; + int cast; + + s = va_arg(val, char *); + if (s == NULL) + s = "(null)"; + for (i = 0; s[i] != '\0'; i++) + { + if (s[i] < 32 || s[i] >= 127) + { + _putchar('\\'); + _putchar('x'); + len = len + 2; + cast = s[i]; + if (cast < 16) + { + _putchar('0'); + len++; + } + len = len + printf_HEX_aux(cast); + } + else + { + _putchar(s[i]); + len++; + } + } + return (len); +} diff --git a/main.h b/main.h index 5f434b3..8878b19 100644 --- a/main.h +++ b/main.h @@ -1,18 +1,47 @@ -#ifndef MAIN_H_ -#define MAIN_H_ +#ifndef MAIN_H +#define MAIN_H + +#include +#include #include -int _printf(const char *format, ...); -int _contains(const char *str, char c); -int _strlen(const char *str); -char *_strcpy(char *dest, char *src); -char *append(char *str, char c); -int _puts(char *str); +#include +#include + +/** + * struct format - match the conversion specifiers + * @id:identifier + * @f: type pointer + */ + +typedef struct format +{ + char *id; + int (*f)(); +} convert_match; + +int printf_pointer(va_list val); +int printf_hex_aux(unsigned long int num); +int printf_HEX_aux(unsigned int num); +int printf_exclusive_string(va_list val); +int printf_HEX(va_list val); +int printf_unsigned(va_list args); +int printf_bin(va_list val); +int printf_srev(va_list args); +int printf_rot13(va_list args); +int printf_int(va_list args); +int printf_hex(va_list val); +int _strlenc(const char *s); +int rev_string(char *s); +int _strlenc(const char *s); +int printf_37(void); +int printf_char(va_list val); +int printf_string(va_list val); int _putchar(char c); -char *_memset(char *adr, int bval); -int fmt(char c, va_list args); -char *_str_reverse(char *str); -char *_itoa(int n, int base); -char *_uitoa(unsigned int n, int base); -char *str_up(char *s); +int _printf(const char *format, ...); +int printf_oct(va_list val); +int printf_dec(va_list args); +int _strlen(char *s); +int *_strcpy(char *dest, char *src); + #endif diff --git a/README.md b/proven-point/README.md similarity index 100% rename from README.md rename to proven-point/README.md diff --git a/itoa.c b/proven-point/itoa.c similarity index 100% rename from itoa.c rename to proven-point/itoa.c diff --git a/proven-point/main.h b/proven-point/main.h new file mode 100644 index 0000000..5f434b3 --- /dev/null +++ b/proven-point/main.h @@ -0,0 +1,18 @@ +#ifndef MAIN_H_ +#define MAIN_H_ +#include +int _printf(const char *format, ...); +int _contains(const char *str, char c); +int _strlen(const char *str); +char *_strcpy(char *dest, char *src); +char *append(char *str, char c); +int _puts(char *str); +int _putchar(char c); +char *_memset(char *adr, int bval); +int fmt(char c, va_list args); +char *_str_reverse(char *str); +char *_itoa(int n, int base); +char *_uitoa(unsigned int n, int base); +char *str_up(char *s); + +#endif diff --git a/memset.c b/proven-point/memset.c similarity index 100% rename from memset.c rename to proven-point/memset.c diff --git a/printf.c b/proven-point/printf.c similarity index 100% rename from printf.c rename to proven-point/printf.c diff --git a/puts.c b/proven-point/puts.c similarity index 100% rename from puts.c rename to proven-point/puts.c diff --git a/strcpy.c b/proven-point/strcpy.c similarity index 100% rename from strcpy.c rename to proven-point/strcpy.c diff --git a/proven-point/strings.c b/proven-point/strings.c new file mode 100644 index 0000000..7480802 --- /dev/null +++ b/proven-point/strings.c @@ -0,0 +1,84 @@ +#include "main.h" +#include + +/** + * _strlen - Takes string and return its length + * + * @str: Address to the head of the string (Array of Characters) (Lost btw) + * + * Return: Length of String + */ +int _strlen(const char *str) +{ + if (*str) + return (1 + _strlen(++str)); + else + return (0); +} + +/** + * _contains - gets number of unique identifiers + * + * @str: da string + * @c: basically almost always '%' + * + * Return: number of unique cases of "%*" that aren't "%%" + */ +int _contains(const char *str, char c) +{ + if (*str) + { + if (*str == c && *(str + 1) != c && *(str - 1) != c) + return (1 + _contains(str + 1, c)); + else + return (_contains(str + 1, c)); + } + else + return (0); +} + +/** + * append - shoved character at the end + * + * @str: main string + * @c: character getting shoved + * + * Return: da string but appended + */ + +char *append(char *str, char c) +{ + int len, i; + char *new_me; + + len = _strlen(str); + + new_me = malloc(len + 1); + i = -1; + while (*(str + ++i)) + *(new_me + i) = *(str + i); + + *(new_me + i++) = c; + *(new_me + len + 1) = '\0'; + return (new_me); +} + +/** + * str_up - changes all lowercase letters of a string + * to uppercase + * @s: string to modify + * + * Return: the resulting string + */ +char *str_up(char *s) +{ + int i; + + for (i = 0; s[i] != '\0'; i++) + { + if (s[i] >= 'a' && s[i] <= 'z') + s[i] = s[i] - 32; + } + + return (s); +} diff --git a/proven-point/tests/a b/proven-point/tests/a new file mode 100755 index 0000000000000000000000000000000000000000..11aa41de55a9f7cd35eb15936de2c296f4ec5baf GIT binary patch literal 16552 zcmeHOdyrGr89xai3!+(YQGAjL+p;2og@tZaG(0X`91tq(v`%a9W|J(Nc9X2hO#ug# z8rC5VJGE17nQ6ztKRRk{9h_>X4&+W$gHGZE@aG4;k6~vY56%y8ha;vU@ghi`32j8>B zW#TOG6D6kPVMQQS<+y(|t<(4fP_mmvl`-fg3KmQ`LZW0hUa3$(C`^U!2iZ*`f#$um zPY1@7<<<6rWQ-D&-&MLt_m8RDjzT%GKT6I3XbP*Y$F!kDXX*(}nt;_^dYJXIZyD})<2ru2KV8N8z`y%Z0 z&}xm3Cp^qI>h{JPhjyKRraHZ1VRxcqSCj@?FvS(b zhvwAU8`=cilnB#l6#m_cXMZGX&~nn>yCihWoWCwT_Ug)g9ltqp?d{*3^78GZLvfQ0 z=};kmJVl77`V@RfN9E&Ns40x-vG|KgqT`i?5X=}uc zgbcWFp7Vs4UAUT`gyMST!nqB?D=wVoHJ4v%d@=wUb1o$pPV0rsVHYm1^ICA!g_EyZ zsb!#+fm#M?8K`BTmVsIZ{@*k3Uf{BSnfbTs%;Kciu{SV#OSa!RYUZD-dse2#S$YTH z(@tX^Brsc$eiunj55D0z&RvqH8?@7d$ErNtD4rgCy2{fH;_1Ohn1{Dr(%Whw{efTL z;Tt{tr5=8!n{WQxy27!qnT5ZY`IAGNwiwN&<|Ag`@)yvmGxNQW5NA3=f!TY&qJw?Q zFNCexH)#fuFI4QA==y6=qOMPI1_HBplistMLVu8ieM|pBr0GMmFl0Xc_I2jdl?kTr zka_Gwy8#YvB=GDGxN*upsHDT)1GD)m){AdJousGlw(NF zp-H>I`)-EqDEq-}5R#)d*kQ2h4;fA*@TB1s4xt|_&n18Tzoe+koA4!TN1k;V1kJ*( zav^uJ-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exp = exp * 10; + num = num / 10; + } + num = n; + while (exp > 0) + { + digit = num / exp; + _putchar(digit + '0'); + num = num - (digit * exp); + exp = exp / 10; + i++; + } + } + _putchar(last + '0'); + + return (i); +} + +#include "main.h" + +/** + * printf_oct - prints an octal number. + * @val: arguments. + * Return: counter. + */ +int printf_oct(va_list val) +{ + int i; + int *array; + int counter = 0; + unsigned int num = va_arg(val, unsigned int); + unsigned int temp = num; + + while (num / 8 != 0) + { + num /= 8; + counter++; + } + counter++; + array = malloc(counter * sizeof(int)); + + for (i = 0; i < counter; i++) + { + array[i] = temp % 8; + temp /= 8; + } + for (i = counter - 1; i >= 0; i--) + { + _putchar(array[i] + '0'); + } + free(array); + return (counter); +} + +/** + * printf_hex_aux - prints an hexgecimal number. + * @num: arguments. + * Return: counter. + */ +int printf_hex_aux(unsigned long int num) +{ + long int i; + long int *array; + long int counter = 0; + unsigned long int temp = num; + + while (num / 16 != 0) + { + num /= 16; + counter++; + } + counter++; + array = malloc(counter * sizeof(long int)); + + for (i = 0; i < counter; i++) + { + array[i] = temp % 16; + temp /= 16; + } + for (i = counter - 1; i >= 0; i--) + { + if (array[i] > 9) + array[i] = array[i] + 39; + _putchar(array[i] + '0'); + } + free(array); + return (counter); +} + +/** + * printf_hex - prints an hexgecimal number. + * @val: arguments. + * Return: counter. + */ +int printf_hex(va_list val) +{ + int i; + int *array; + int counter = 0; + unsigned int num = va_arg(val, unsigned int); + unsigned int temp = num; + + while (num / 16 != 0) + { + num /= 16; + counter++; + } + counter++; + array = malloc(counter * sizeof(int)); + + for (i = 0; i < counter; i++) + { + array[i] = temp % 16; + temp /= 16; + } + for (i = counter - 1; i >= 0; i--) + { + if (array[i] > 9) + array[i] = array[i] + 39; + _putchar(array[i] + '0'); + } + free(array); + return (counter); +} diff --git a/special.c b/special.c new file mode 100644 index 0000000..07f5ec0 --- /dev/null +++ b/special.c @@ -0,0 +1,121 @@ +#include "main.h" +/** + * printf_37 - prints the char 37. + * Return: 1. + */ +int printf_37(void) +{ + _putchar(37); + return (1); +} + +/** + * printf_rot13 - printf str to ROT13 place into buffer + * @args: type struct va_arg where is allocated printf arguments + * Return: counter + */ +int printf_rot13(va_list args) +{ + int i, j, counter = 0; + int k = 0; + char *s = va_arg(args, char*); + char alpha[] = {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"}; + char beta[] = {"nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM"}; + + if (s == NULL) + s = "(null)"; + for (i = 0; s[i]; i++) + { + k = 0; + for (j = 0; alpha[j] && !k; j++) + { + if (s[i] == alpha[j]) + { + _putchar(beta[j]); + counter++; + k = 1; + } + } + if (!k) + { + _putchar(s[i]); + counter++; + } + } + return (counter); +} + +/** + * printf_pointer - prints an hexgecimal number. + * @val: arguments. + * Return: counter. + */ +int printf_pointer(va_list val) +{ + void *p; + char *s = "(nil)"; + long int a; + int b; + int i; + + p = va_arg(val, void*); + if (p == NULL) + { + for (i = 0; s[i] != '\0'; i++) + { + _putchar(s[i]); + } + return (i); + } + + a = (unsigned long int)p; + _putchar('0'); + _putchar('x'); + b = printf_hex_aux(a); + return (b + 2); +} + +/** + * printf_int - prints integer + * @args: argument to print + * Return: number of characters printed + */ + +int printf_int(va_list args) +{ + int n = va_arg(args, int); + int num, last = n % 10, digit, exp = 1; + int i = 1; + + n = n / 10; + num = n; + + if (last < 0) + { + _putchar('-'); + num = -num; + n = -n; + last = -last; + i++; + } + if (num > 0) + { + while (num / 10 != 0) + { + exp = exp * 10; + num = num / 10; + } + num = n; + while (exp > 0) + { + digit = num / exp; + _putchar(digit + '0'); + num = num - (digit * exp); + exp = exp / 10; + i++; + } + } + _putchar(last + '0'); + + return (i); +} diff --git a/strings.c b/strings.c index 7480802..7791d05 100644 --- a/strings.c +++ b/strings.c @@ -1,84 +1,120 @@ #include "main.h" -#include /** - * _strlen - Takes string and return its length - * - * @str: Address to the head of the string (Array of Characters) (Lost btw) - * - * Return: Length of String + * printf_char - prints a char. + * @val: arguments. + * Return: 1. */ -int _strlen(const char *str) +int printf_char(va_list val) { - if (*str) - return (1 + _strlen(++str)); - else - return (0); + char s; + + s = va_arg(val, int); + _putchar(s); + return (1); } /** - * _contains - gets number of unique identifiers - * - * @str: da string - * @c: basically almost always '%' - * - * Return: number of unique cases of "%*" that aren't "%%" + * printf_bin - prints a binary number. + * @val: arguments. + * Return: 1. */ -int _contains(const char *str, char c) +int printf_bin(va_list val) { - if (*str) + int flag = 0; + int cont = 0; + int i, a = 1, b; + unsigned int num = va_arg(val, unsigned int); + unsigned int p; + + for (i = 0; i < 32; i++) { - if (*str == c && *(str + 1) != c && *(str - 1) != c) - return (1 + _contains(str + 1, c)); - else - return (_contains(str + 1, c)); + p = ((a << (31 - i)) & num); + if (p >> (31 - i)) + flag = 1; + if (flag) + { + b = p >> (31 - i); + _putchar(b + 48); + cont++; + } } - else - return (0); + if (cont == 0) + { + cont++; + _putchar('0'); + } + return (cont); } - /** - * append - shoved character at the end - * - * @str: main string - * @c: character getting shoved - * - * Return: da string but appended + * printf_HEX - prints an hexgecimal number. + * @val: arguments. + * Return: counter. */ - -char *append(char *str, char c) -{ - int len, i; - char *new_me; - - len = _strlen(str); - - new_me = malloc(len + 1); - i = -1; - while (*(str + ++i)) - *(new_me + i) = *(str + i); - - *(new_me + i++) = c; - *(new_me + len + 1) = '\0'; - return (new_me); -} - -/** - * str_up - changes all lowercase letters of a string - * to uppercase - * @s: string to modify - * - * Return: the resulting string - */ -char *str_up(char *s) +int printf_HEX(va_list val) { int i; + int *arr; + int counter = 0; + unsigned int num = va_arg(val, unsigned int); + unsigned int temp = num; - for (i = 0; s[i] != '\0'; i++) + while (num / 16 != 0) { - if (s[i] >= 'a' && s[i] <= 'z') - s[i] = s[i] - 32; + num /= 16; + counter++; } + counter++; + arr = malloc(counter * sizeof(int)); - return (s); + for (i = 0; i < counter; i++) + { + arr[i] = temp % 16; + temp /= 16; + } + for (i = counter - 1; i >= 0; i--) + { + if (arr[i] > 9) + arr[i] = arr[i] + 7; + _putchar(arr[i] + '0'); + } + free(arr); + return (counter); +} + +#include "main.h" + +/** + * printf_HEX_aux - prints an hexdecimal number. + * @num: number to print. + * Return: counter. + */ +int printf_HEX_aux(unsigned int num) +{ + int i; + int *arr; + int counter = 0; + unsigned int temp = num; + + while (num / 16 != 0) + { + num /= 16; + counter++; + } + counter++; + arr = malloc(counter * sizeof(int)); + + for (i = 0; i < counter; i++) + { + arr[i] = temp % 16; + temp /= 16; + } + for (i = counter - 1; i >= 0; i--) + { + if (arr[i] > 9) + arr[i] = arr[i] + 7; + _putchar(arr[i] + '0'); + } + free(arr); + return (counter); } diff --git a/strs2.c b/strs2.c new file mode 100644 index 0000000..b36c75b --- /dev/null +++ b/strs2.c @@ -0,0 +1,122 @@ +#include "main.h" +/** + * _strlen - Returns the lenght of a string. + * @s: Type char pointer + * Return: c. + */ +int _strlen(char *s) +{ + int c; + + for (c = 0; s[c] != 0; c++) + ; + return (c); + +} +/** + * _strlenc - Strlen function but applied for constant char pointer s + * @s: Type char pointer + * Return: c + */ +int _strlenc(const char *s) +{ + int c; + + for (c = 0; s[c] != 0; c++) + ; + return (c); +} + +/** + * printf_string - print a string. + * @val: argumen t. + * Return: the length of the string. + */ + +int printf_string(va_list val) +{ + char *s; + int i, len; + + s = va_arg(val, char *); + if (s == NULL) + { + s = "(null)"; + len = _strlen(s); + for (i = 0; i < len; i++) + _putchar(s[i]); + return (len); + } + else + { + len = _strlen(s); + for (i = 0; i < len; i++) + _putchar(s[i]); + return (len); + } +} + +/** + * printf_unsigned - prints integer + * @args: argument to print + * Return: number of characters printed + */ +int printf_unsigned(va_list args) +{ + unsigned int n = va_arg(args, unsigned int); + int num, last = n % 10, digit, exp = 1; + int i = 1; + + n = n / 10; + num = n; + + if (last < 0) + { + _putchar('-'); + num = -num; + n = -n; + last = -last; + i++; + } + if (num > 0) + { + while (num / 10 != 0) + { + exp = exp * 10; + num = num / 10; + } + num = n; + while (exp > 0) + { + digit = num / exp; + _putchar(digit + '0'); + num = num - (digit * exp); + exp = exp / 10; + i++; + } + } + _putchar(last + '0'); + + return (i); +} + +/** + * printf_srev - function that prints a str in reverse + * @args: type struct va_arg where is allocated printf arguments + * Return: the string + */ +int printf_srev(va_list args) +{ + + char *s = va_arg(args, char*); + int i; + int j = 0; + + if (s == NULL) + s = "(null)"; + while (s[j] != '\0') + j++; + for (i = j - 1; i >= 0; i--) + _putchar(s[i]); + return (j); +}