Answer the question
In order to leave comments, you need to log in
How to add "prefix" to argv[1] argument in C?
How to add "prefix" to argv[1] argument in C?
A string comes into the program as the second argument, let's do "name".
How to add "my" to it?
I implemented the algorithm for shifting the array to the right, I shift the array and first add a prefix, but I need to know the length of the incoming array , which is the problem
void arrayShift(char array[], int len, int offset) {
int last = len - 1;
for ( int i = last; i >= 0; i-- ) {
array[i+offset] = array[i];
}
}
arrayShift(argv[1], size, 2);
argv[1][0] = 'm';
argv[1][1] = 'y';
prinf("%s\n", argv[1]);
Answer the question
In order to leave comments, you need to log in
char *p = malloc(strlen(argv[2]+4));
sprintf(p, "my %s", argv[2]);
argv[2] = p;
string.h library for C
function strlen( const char * string );
will return the number of characters in the string
, the strcat function for appending to the string
char * strcat( char * destptr, const char * srcptr );
those.
charbuf*="my";
strcat(buf,argv[1]);
argv[1]=buf;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question