summaryrefslogtreecommitdiff
path: root/ex1-9.c
diff options
context:
space:
mode:
Diffstat (limited to 'ex1-9.c')
-rw-r--r--ex1-9.c22
1 files changed, 22 insertions, 0 deletions
diff --git a/ex1-9.c b/ex1-9.c
new file mode 100644
index 0000000..e7d4d25
--- /dev/null
+++ b/ex1-9.c
@@ -0,0 +1,22 @@
+#include <stdio.h>
+#include <stdbool.h>
+/* Copy stdin to stdout, replacing consecutive spaces with a single space
+ Edge case: a leading space is retained if there are leading spaces.
+ Edge case: an empty file just doesn't echo anything, but emits a warning on stderr */
+int main(void) {
+ int c = getchar();
+ if (c == EOF) {
+ fprintf(stderr, "warning: no input\n");
+ goto exit;
+ }
+ putchar(c);
+ bool bl = (c == ' ');
+ while ((c = getchar()) != EOF) {
+ if ((!bl) || (c != ' ')) {
+ putchar(c);
+ }
+ bl = (c == ' ');
+ }
+exit:
+ return 0;
+} \ No newline at end of file