commit 8ae5186f8848c35104303b8c99146f0b80fac586
parent 75fef584b8d767e6f175875be75bed8f248b4211
Author: klewer-martin <martin.cachari@gmail.com>
Date: Wed, 23 Jun 2021 01:47:08 -0300
Update: added guia08 exercises
Diffstat:
8 files changed, 242 insertions(+), 19 deletions(-)
diff --git a/guia08/ex01.c b/guia08/ex01.c
@@ -1,4 +1,4 @@
-// Clones a given string entered as arguments of function main;
+/* Clones a given string entered as arguments of function main; */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -21,26 +21,22 @@ int main (int argc, char * argv[])
size_t i;
status_t st;
-// Verify if arguments are right;
- if((st = validar_argumentos(argc, argv) != OK))
- return st;
+ /* Verify if arguments are right; */
+ if((st = validar_argumentos(argc, argv))) return st;
-// Allocates memmory in heap of size INIT_SIZE;
- if((dest = (char *)malloc(INIT_SIZE * sizeof(char))) == NULL)
- return ERROR_ALLOC_MEMORY;
+ /* Allocates memmory in heap of size INIT_SIZE; */
+ if(!(dest = (char *)malloc(INIT_SIZE * sizeof(char))))
+ return ERROR_ALLOC_MEMORY;
-// Assigns 1 to i to avoid the first element of argv,
-// which is the program name. Then copies every string of argv
-// into dest and prints it on stdout;
+ /* Assigns 1 to i to avoid the first element of argv, which is the program name. Then copies every string of argv into dest and prints it on stdout; */
for(i = 1; (int)i < argc; i++) {
strcpy(dest, argv[i]);
- // Puts a space in between strings, avoiding a blank
- // space after first string is printed;
+ /* Puts a space in between strings, avoiding a blank space after first string is printed; */
if(i != 1) putchar(' ');
printf("%s", dest);
}
-// Adds the new line character;
+ /* Adds the new line character; */
putchar('\n');
free(dest);
return OK;
@@ -48,11 +44,8 @@ int main (int argc, char * argv[])
status_t validar_argumentos(int argc, char **argv)
{
- if(argc == NO_ARGUMENT)
- return ERROR_PROGRAM_INVOCATION;
+ if(argc == NO_ARGUMENT) return ERROR_PROGRAM_INVOCATION;
+ if(!argv) return ERROR_NULL_POINTER;
- else if(argv == NULL)
- return ERROR_NULL_POINTER;
-
- else return OK;
+ return OK;
}
diff --git a/guia08/ex02.c b/guia08/ex02.c
@@ -0,0 +1,32 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+char *left_trim(const char *);
+
+int main (void)
+{
+ char str[] = " Hola mundo";
+
+ printf("%s\n", str);
+
+ printf("%s\n", left_trim(str));
+
+ return 0;
+}
+
+char *left_trim(const char *src)
+{
+ char *dst;
+
+ /* Counts blank spaces */
+ size_t i = 0;
+ while(src[i] == ' ') i++;
+
+ /* +1 for the null character */
+ if(!(dst = (char *)malloc((strlen(src) - i + 1) * sizeof(char))))
+ return NULL;
+
+ strcpy(dst, src + i);
+ return dst;
+}
diff --git a/guia08/ex03.c b/guia08/ex03.c
@@ -0,0 +1,38 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+char *right_trim(const char *);
+
+int main (void)
+{
+ char str[] = "Hola mundo ";
+ char *dst;
+
+ printf("%s\n", str);
+ printf("len %ld\n", strlen(str));
+
+ dst = right_trim(str);
+ printf("%s\n", dst);
+ printf("len %ld\n", strlen(dst));
+
+ free(dst);
+ return 0;
+}
+
+char *right_trim(const char *src)
+{
+ char *dst;
+ size_t i;
+
+ /* Counts blank spaces */
+ for(i = strlen(src); src[i - 1] == ' '; i--);
+
+ /* +1 for the null character */
+ if(!(dst = (char *)malloc((i + 1) * sizeof(char))))
+ return NULL;
+
+ strncpy(dst, src, i);
+ dst[i] = '\0';
+ return dst;
+}
diff --git a/guia08/ex04.c b/guia08/ex04.c
@@ -0,0 +1,38 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+char *full_trim(const char *);
+
+int main (void)
+{
+ char str[] = " Hello world!";
+ char *dst;
+
+ printf("%s\nlen %ld\n", str, strlen(str));
+ dst = full_trim(str);
+ printf("%s\nlen %ld\n", dst, strlen(dst));
+
+ return 0;
+}
+
+char *full_trim(const char *src)
+{
+ char *dst;
+ size_t i, j, len;
+
+ len = strlen(src);
+
+ /* Counts starting blank spaces */
+ for(i = 0; src[i] == ' '; i++);
+
+ /* Counts ending blank spaces */
+ for(j = len; src[j - 1] == ' '; j--);
+
+ if(!(dst = (char *)calloc(j - i, sizeof(char))))
+ return NULL;
+
+ strcpy(dst, src + i);
+ dst[j - i] = '\0';
+ return dst;
+}
diff --git a/guia08/ex05.c b/guia08/ex05.c
@@ -0,0 +1,26 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#define INT_MAX_DIGITS 5
+
+char *itoa(int n);
+
+int main (void)
+{
+ char *arr;
+ arr = itoa(32211);
+ printf("%s\n", arr);
+
+ free(arr);
+ return 0;
+}
+
+char *itoa(int n)
+{
+ char *arr;
+
+ arr = (char *)calloc(INT_MAX_DIGITS + 1, sizeof(char));
+ sprintf(arr, "%d", n);
+
+ return arr;
+}
diff --git a/guia08/ex06.c b/guia08/ex06.c
@@ -0,0 +1,26 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#define INT_MAX_OCT_DIGITS 5
+
+char *itoo(int n);
+
+int main (void)
+{
+ char *arr;
+ arr = itoo(32211);
+ printf("%s\n", arr);
+
+ free(arr);
+ return 0;
+}
+
+char *itoo(int n)
+{
+ char *arr;
+
+ arr = (char *)calloc(INT_MAX_OCT_DIGITS + 1, sizeof(char));
+ sprintf(arr, "%o", n);
+
+ return arr;
+}
diff --git a/guia08/ex07.c b/guia08/ex07.c
@@ -0,0 +1,26 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#define INT_MAX_HEX_DIGITS 4
+
+char *itoo(int n);
+
+int main (void)
+{
+ char *arr;
+ arr = itoo(32155);
+ printf("%s\n", arr);
+
+ free(arr);
+ return 0;
+}
+
+char *itoo(int n)
+{
+ char *arr;
+
+ arr = (char *)calloc(INT_MAX_HEX_DIGITS + 1, sizeof(char));
+ sprintf(arr, "%X", n);
+
+ return arr;
+}
diff --git a/guia08/ex17.c b/guia08/ex17.c
@@ -0,0 +1,44 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#define INIT_SIZE 10
+#define GROWTH_FACTOR .5
+
+char *read_line(void);
+
+int main (void)
+{
+ char *str = NULL;
+
+ str = read_line();
+ printf("%s", str);
+
+ free(str);
+ return 0;
+}
+
+char *read_line(void)
+{
+ char *dst;
+ int aux;
+ size_t i, alloc_size;
+
+ alloc_size = INIT_SIZE;
+
+ if(!(dst = (char *)calloc(alloc_size, sizeof(char)))) return NULL;
+ printf("allocating: %ld bytes\n", alloc_size * sizeof(char));
+
+ for(i = 0; (aux = fgetc(stdin)) != EOF; i++)
+ {
+ if(i == (alloc_size - 2))
+ {
+ printf("\nReallocating\ni: %ld\nold size: %ld\n", i, alloc_size);
+ alloc_size += (alloc_size * GROWTH_FACTOR);
+ printf("new size: %ld\n", alloc_size);
+ if(!(dst = (char *)realloc(dst, alloc_size * sizeof(char)))) return NULL;
+ for(size_t j = i; j < alloc_size; j++) dst[j] = '\0';
+ }
+ dst[i] = aux;
+ }
+ return dst;
+}