c-first-steps

a C playground
Index Commits Files Refs README
commit 6c445c8f075c7749231cd5a83488606fe4f69ffc
parent aabc3780e72e11d2fe81acf0f1fc4bf3f7a6bd8b
Author: Martin J. Klöckner <martin.cachari@gmail.com>
Date:   Fri, 20 Nov 2020 23:32:13 -0300

Added ex13.c, ex14.c & ex1x15.c(unfinished)

Diffstat:
A95.11/guia03/ex13.c | 61+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A95.11/guia03/ex14.c | 35+++++++++++++++++++++++++++++++++++
A95.11/guia03/ex15.c | 41+++++++++++++++++++++++++++++++++++++++++
3 files changed, 137 insertions(+), 0 deletions(-)
diff --git a/95.11/guia03/ex13.c b/95.11/guia03/ex13.c
@@ -0,0 +1,61 @@
+//    Reads a string from stdin and converts it to uppercase or lowercase 
+//    depending on an option readed from stdin
+
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+
+#define MSG_STR_PROMPT    "Introduzca una cadena:"
+#define MSG_FMT_PROMPT    "Convertir a: (0 - MAYUSCULAS / 1 - MINUSCULAS)"
+#define MSG_FMT_ERR        "Err: Formato incorrecto"
+
+#define MAX_STR_LEN 100
+
+typedef enum {
+    FMT_MAYUSCULAS, FMT_MINUSCULAS
+} format_t;
+
+
+int main ( void ) {
+
+
+    char buffer[MAX_STR_LEN];
+    char str[MAX_STR_LEN];
+    format_t format;
+
+
+    printf(MSG_STR_PROMPT"\n>> ");
+    fgets(buffer, MAX_STR_LEN, stdin);
+    strcpy(str, buffer);
+
+    printf(MSG_FMT_PROMPT"\n>> ");
+    format = (getchar () - '0');
+    if((format < 0) || (format > 1)) {
+        fprintf(stderr, MSG_FMT_ERR"\n");
+        return 1;
+    }
+
+    printf("La cadena ingresada: %sOpcion elegida: %d\n", str, format);            
+
+    int i, aux;
+    switch(format)
+    {
+        case FMT_MAYUSCULAS: 
+            for(i = 0; (aux = str[i]) != '\n'; i++) {
+                if(islower(aux))
+                    str[i] = toupper(aux);
+            }
+            break;
+        case FMT_MINUSCULAS:
+            for(i = 0; (aux = str[i]) != '\n'; i++) {
+                if(isupper(aux))
+                    str[i] = tolower(aux);
+            }
+            break;
+    }
+
+    printf("%s", str);
+    return 0;
+}
diff --git a/95.11/guia03/ex14.c b/95.11/guia03/ex14.c
@@ -0,0 +1,35 @@
+#include <stdio.h>
+
+
+typedef enum {
+    OK, ERROR
+} status_t; 
+
+
+size_t rechar(char *str, char old, char new);
+
+
+int main( void ) {
+
+    char str[] = "Hello world!";
+
+    rechar(str, 'o', 'e');
+
+    printf("%s\n", str);
+    return OK;
+}
+
+
+
+size_t rechar(char *str, char old, char new) {
+    if(str == NULL)
+        return ERROR;
+
+    int i;
+    for(i = 0; str[i] != '\0'; i++) {
+        if(str[i] == old) {
+            str[i] = new;
+        }
+    }
+    return OK;
+}
diff --git a/95.11/guia03/ex15.c b/95.11/guia03/ex15.c
@@ -0,0 +1,41 @@
+#include <stdio.h>
+#include <string.h>
+
+#define MAX_LEN 100
+
+typedef enum {
+    OK, ERROR
+} status_t;
+
+//    status_t left_trim(str);
+
+int main( void ) {
+
+    char buffer[MAX_LEN];
+    int i, j, len;
+
+    if((fgets(buffer, MAX_LEN, stdin)) == NULL)
+        return ERROR;
+
+    len = strlen(buffer);
+    printf("%d\n", len);
+
+    char str[len - 1];
+    char aux;
+
+    for(i = 0; buffer[i] == ' '; i++) {
+        //for(j = 0; buffer[j] == ' '; j++)
+        //    str[i] = buffer[j];
+        //    printf("for\n");
+        str[i] = buffer[i];
+        if(buffer[i] == ' ') {
+            str[i] = buffer[i + 1];
+            i++;
+        }
+        //str[i] = buffer[j];
+    }
+    
+    printf("%s\n", str);
+
+    return OK;
+}