c-first-steps

a C playground
Index Commits Files Refs README
commit 7e0d89725046ab2df5d7f11461078dcad54ba3de
parent 1c57c42c1f3747c7e3125b61cf6d70182127a8e4
Author: Martin J. Klöckner <martin.cachari@gmail.com>
Date:   Sat, 28 Nov 2020 23:59:04 -0300

Updated ex26.c (not finished yet)

Diffstat:
M95.11/guia03/ex26.c | 61++++++++++++++++++++++++++++++++++++++++---------------------
1 file changed, 40 insertions(+), 21 deletions(-)
diff --git a/95.11/guia03/ex26.c b/95.11/guia03/ex26.c
@@ -3,39 +3,59 @@
 
 #define MAX_LEN 100
 
+
+
 int main ( void ) {
 
-    char buffer[MAX_LEN], str1[MAX_LEN], str2[MAX_LEN], dest[MAX_LEN];
+    char buffer[MAX_LEN], str1[MAX_LEN], str2[MAX_LEN];
+    char dest[MAX_LEN];
     int i, j, num;
 
-    if(fgets(str1, MAX_LEN, stdin) == NULL)
+    if(fgets(str1, MAX_LEN, stdin) == NULL) {
         return 1;
+    }
+
+//  This part couts the lengh of the first string
+//    and removes the new line character;
+    for(i = 0; str1[i] != '\0'; i++)
+        if(str1[i] == '\n')
+            str1[i] = str1[i + 1];
+
+    printf("str1 length = %d\n", i);
 
-    if(fgets(str2, MAX_LEN, stdin) == NULL)
+    if(fgets(str2, MAX_LEN, stdin) == NULL) {
         return 1;
+    }
 
-    if(fgets(buffer, MAX_LEN, stdin) == NULL)
+    if(fgets(buffer, MAX_LEN, stdin) == NULL) {
        return 1;
+    }
 
     num = atoi(buffer);
+    printf("str1 = %s\n", str1);
+    printf("str2 = %s\n", str2);
+    printf("num = %d\n", num);
 
-//  This part couts the lengh of the first string;
-    for(i = 0; str1[i] != '\0'; i++)
-        ;
-    
 //  This part checks if the input number is bigger than
 //  the length of the string;
-    if(num > i)
+    if(num > i) {
         return 1;
-
-    for(i = num, j = 0; str2[i] != '\0'; j++)
-        if(j <= num)
-            dest[j] = str1[j];
-
-        else 
-            dest[i] = str2[i];
-            i++;
-
-    printf("%s\n", dest);
+    }
+
+    for(i = 0; str2[i] != '\0'; i++)
+        if(str2[i] == '\n')
+            str2[i] = '\0';
+
+    for(i = 0, j = 0; str2[j] != '\0'; i++) {
+        if(i <= num) {
+            dest[i] = str1[i];
+        } else { 
+            dest[i] = str2[j];
+            j++;
+        }
+    }
+//    printf("%d\n", i);
+//    printf("%d\n", j);
+//    printf("%s\n", dest);
     return 0;
-}
-\ No newline at end of file
+}