c-first-steps

a C playground
Index Commits Files Refs README
commit ea1b18af7c7dae892fe778c71af208b09a284a40
parent 6530eb9f6481db1549fa1785dbad5a62167af063
Author: Martin J. Klöckner <martin.cachari@gmail.com>
Date:   Tue, 24 Nov 2020 08:35:56 -0300

Added ex16.c

Diffstat:
A95.11/guia03/ex16.c | 28++++++++++++++++++++++++++++
1 file changed, 28 insertions(+), 0 deletions(-)
diff --git a/95.11/guia03/ex16.c b/95.11/guia03/ex16.c
@@ -0,0 +1,28 @@
+//    Reads a string from stdin and if it ends in blank
+//    spaces it erase them and moves the '\n', '\0' chars
+//    it performs a "right trim"
+
+
+#include <stdio.h>
+
+#define MAX_LEN 100
+
+
+int main ( void ) {
+
+    int i, j, k;
+    char str[MAX_LEN];
+
+    if(fgets(str, MAX_LEN, stdin) == NULL)
+        return 1;
+
+    for(i = 0; str[i] != '\0'; i++) 
+        if(str[i + 1] == '\n') 
+            for(j = 0; str[i - j] == ' '; j++)
+                ;
+            
+    str[(i - j) - 1] = str[i - 1];
+    str[(i - j)] = '\0';
+
+    return 0;
+}