summaryrefslogtreecommitdiff
path: root/nt.c
diff options
context:
space:
mode:
Diffstat (limited to 'nt.c')
-rw-r--r--nt.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/nt.c b/nt.c
new file mode 100644
index 0000000..bc00216
--- /dev/null
+++ b/nt.c
@@ -0,0 +1,48 @@
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+
+/// Prints *str* *times* to standard output.
+int
+repeat(const int times, const char* str)
+{
+ for (int i = 0; i < times; ++i)
+ printf("%s", str);
+ return printf("\n");
+}
+
+
+/// Concatenates strings from *strv*. User needs to free allocated string.
+char*
+concat(const int strc, const char* strv[])
+{
+ assert(0 < strc);
+ int i;
+ int len = 0;
+ for (i = 0; i < strc; ++i)
+ len += 1 + strlen(strv[i]);
+ char* const str = malloc(len);
+ str[0] = 0;
+ char* ptr = stpcpy(str, strv[0]);
+ for (int i = 1; i < strc; ++i) {
+ *ptr = ' ';
+ *++ptr = 0;
+ ptr = stpcpy(ptr, strv[i]);
+ }
+ return str;
+}
+
+
+int
+main(const int argc, const char* argv[])
+{
+ if (3 > argc)
+ return 1;
+ const int times = atoi(argv[1]);
+ if (1 > times)
+ return 1;
+ const char* str = concat(argc - 2, &argv[2]);
+ return repeat(times, str);
+}