Answer the question
In order to leave comments, you need to log in
How to pass a string that ends with: !x as a command line argument?
Basically, I wrote this code:
#include <stdio.h>
#include <stdlib.h>
size_t str_copy2buf(size_t n, char buf[n], const char str[])
{
size_t i;
for (i = 0; i < n; i++) {
if ((buf[i] = str[i]) == '\0')
break;
}
return i;
}
size_t str_append2buf(size_t n, char buf[n], const char str[])
{
size_t i, j;
for (i = 0; i < n; i++) {
if (buf[i] == '\0')
break;
}
for (j = 0; i < n; i++) {
if ((buf[i] = str[j]) == '\0')
break;
j++;
}
return i;
}
int main(int argc, char *argv[argc+1])
{
char buf[15];
size_t index;
index = str_copy2buf(sizeof(buf), buf, argv[1]);
if (index == sizeof(buf)) {
fprintf(stderr, "error: copy failed!\n");
return EXIT_FAILURE;
}
index = str_append2buf(sizeof(buf), buf, argv[2]);
if (index == sizeof(buf)) {
fprintf(stderr, "error: append failed!\n");
return EXIT_FAILURE;
}
printf("%s\n", buf);
return EXIT_SUCCESS;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question